Skip to content

Commit 69a67a1

Browse files
committed
New solution of MaxDepthTree
1 parent b8cfe55 commit 69a67a1

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'''
2+
Maximum Depth of Binary Tree
3+
Given the root of a binary tree, return its maximum depth.
4+
5+
A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
6+
7+
'''
8+
# Definition for a binary tree node.
9+
# class TreeNode:
10+
# def __init__(self, val=0, left=None, right=None):
11+
# self.val = val
12+
# self.left = left
13+
# self.right = right
14+
class Solution:
15+
def maxDepth(self, root: Optional[TreeNode]) -> int:
16+
17+
if root is None:
18+
return 0
19+
else:
20+
# Compute the depth of each subtree
21+
lDepth = self.maxDepth(root.left)
22+
rDepth = self.maxDepth(root.right)
23+
24+
if rDepth > lDepth:
25+
return rDepth+1
26+
else:
27+
return lDepth+1
28+
29+

0 commit comments

Comments
 (0)