Skip to content

Commit 3b7372a

Browse files
committed
maximum depth of binary tree solution
1 parent 127714c commit 3b7372a

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
return self.dfs(root, 0)
10+
11+
def dfs(self, root, depth) -> int:
12+
if not root:
13+
return depth
14+
15+
return max(self.dfs(root.left, depth + 1), self.dfs(root.right, depth + 1))

0 commit comments

Comments
 (0)