Skip to content

Commit 97c514d

Browse files
committed
maximum-depth-of-binary-tree
1 parent 7fd6cd4 commit 97c514d

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)