We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent ae67756 commit 6a14065Copy full SHA for 6a14065
maximum-depth-of-binary-tree/mandel-17.py
@@ -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