Tree DFS
LC #104Easy
Maximum Depth of Binary Tree
Tree DFS
AmazonGoogleLinkedInMetaProblem
Find the maximum depth (number of nodes along the longest path from root to a leaf).
treedfsrecursion
Constraints
- ›0 ≤ n ≤ 10⁴
- ›-100 ≤ Node.val ≤ 100
Example
Input
root = [3, 9, 20, null, null, 15, 7]Output
3Why
Longest path: 3→20→15 or 3→20→7, both length 3