We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent b8cfe55 commit 69a67a1Copy full SHA for 69a67a1
coding_solutions/DataStructure_related/Tree/MaxDepthTree.py
@@ -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
27
+ return lDepth+1
28
29
0 commit comments