Skip to content

Commit 6a14065

Browse files
committed
Week 4: maximum-depth-of-binary-tree solution
1 parent ae67756 commit 6a14065

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import collections
2+
from typing import Optional
3+
4+
class TreeNode:
5+
def __init__(self, val=0, left=None, right=None):
6+
self.val = val
7+
self.left = left
8+
self.right = right
9+
10+
class Solution:
11+
def maxDepth(self, root: Optional[TreeNode]) -> int:
12+
if root is None:
13+
return 0
14+
queue = collections.deque([root])
15+
depth = 0
16+
17+
while queue:
18+
depth += 1
19+
for _ in range(len(queue)):
20+
current_root = queue.popleft()
21+
if current_root.left:
22+
queue.append(current_root.left)
23+
if current_root.right:
24+
queue.append(current_root.right)
25+
return depth
26+

0 commit comments

Comments
 (0)