Maximum Depth of a Binary Tree
In this problem, you are given a binary tree and your task is to determine the maximum depth of the tree. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. If the tree is empty, the maximum depth is 0.
[ [ 1, null, 2, null, null, 3 ] ]
Explanation. The binary tree looks like this: `1 -> null -> 2 -> null -> 3`. Here, the maximum depth is 3.
[ [ 3, 9, 20, null, null, 15, 7 ] ]
Explanation. The binary tree looks like this: `3, 9, 20, null, null, 15, 7`. The tree's structure: 3 / \ 9 20 / \ 15 7 Maximum depth is 3.
[ [] ]
Explanation. An empty tree has a depth of 0.
Follow-up: How would you optimize your solution if the tree is very deep and recursion might cause a stack overflow?
The tree nodes' values will be integer values.
- Views
- 2