- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_104.py
More file actions
Latest commit
21 lines (18 loc) · 843 Bytes
/
Copy pathproblem_104.py
File metadata and controls
21 lines (18 loc) · 843 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
"""
Problem: https://leetcode.com/problems/maximum-depth-of-binary-tree/submissions/
Solution: We recursively find out the maximum of the heights of both left and right sub-trees of each node in the Binary Tree from bottom to top.
At the end we'll be havin the maximum depth of the Binary Tree.
Time Complexity: O(n) as we're visting each node in the tree once.
Space Complexity: Since we're not creating any additional Data Structures it would be O(1).
"""
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
classSolution:
defmaxDepth(self, root: Optional[TreeNode]) ->int:
ifnotroot:
return0
returnmax(self.maxDepth(root.left), self.maxDepth(root.right)) +1