Skip to content

Commit af39f1f

Browse files
committed
maximum depth of binary tree solution
1 parent 425459b commit af39f1f

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

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+
# Recursive approach
10+
11+
if not root:
12+
return 0
13+
14+
left_depth = self.maxDepth(root.left)
15+
right_depth = self.maxDepth(root.right)
16+
17+
return max(left_depth, right_depth) + 1
18+
19+
# Time Complexity: O(n)
20+
# Space Complexity: O(n)

0 commit comments

Comments
 (0)