We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 425459b commit af39f1fCopy full SHA for af39f1f
1 file changed
maximum-depth-of-binary-tree/jylee2033.py
@@ -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