Depth of a Tree
You are given the root of a binary tree. Return its depth: the number of nodes on the longest path from the root down to a leaf. An empty tree has depth 0.
Trees in the examples are written level by level, left to right, with null where a child is missing: [1,null,2] is a root 1 with only a right child, 2. [] is an empty tree, with no root at all.
Constraints
- 0 ≤ number of nodes ≤ 10,000
- -100 ≤ node value ≤ 100
- The tree is at most 1,000 levels deep.
Example 1
Input
root = [3,9,20,null,null,15,7]
Output
3
Why:
The longest paths, 3 → 20 → 15 and 3 → 20 → 7, have three nodes.
Example 2
Input
root = [1,null,2]
Output
2
Why:
1 → 2.
Example 3
Input
root = []
Output
0
Why:
An empty tree has no levels.
Hints
Was this a good challenge?
- Solved by
- 0
- Submissions
- 0
- Accepted
- –