Skip to content

Commit 17bd8bb

Browse files
committed
feat: adds max_depth_binary_tree exercise
1 parent 7abff63 commit 17bd8bb

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

max_depth_binary_tree.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import Optional
2+
3+
4+
# Definition for a binary tree node.
5+
class TreeNode:
6+
def __init__(self, val=0, left=None, right=None):
7+
self.val = val
8+
self.left = left
9+
self.right = right
10+
11+
12+
class Solution:
13+
def maxDepth(self, root: Optional[TreeNode]) -> int:
14+
if not root:
15+
return 0
16+
else:
17+
left_depth = self.maxDepth(root.left)
18+
right_depth = self.maxDepth(root.right)
19+
return max(left_depth, right_depth) + 1

0 commit comments

Comments
 (0)