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 7fd6cd4 commit 97c514dCopy full SHA for 97c514d
maximum-depth-of-binary-tree/changhyumm.py
@@ -0,0 +1,20 @@
1
+# Definition for a binary tree node.
2
+# class TreeNode:
3
+# def __init__(self, val=0, left=None, right=None):
4
+# self.val = val
5
+# self.left = left
6
+# self.right = right
7
+class Solution:
8
+ def maxDepth(self, root: Optional[TreeNode]) -> int:
9
+ if root is None:
10
+ return 0
11
+ stack = [[root, 1]]
12
+ max_depth = 1
13
+ while stack:
14
+ node, cur_depth = stack.pop()
15
+ max_depth = max(max_depth, cur_depth)
16
+ if node.left:
17
+ stack.append([node.left, cur_depth + 1])
18
+ if node.right:
19
+ stack.append([node.right, cur_depth + 1])
20
+ return max_depth
0 commit comments