Maximum Depth of Binary Tree
EasyTrees
Given the root of a binary tree, return its maximum depth: the number of nodes on the longest path from the root down to a leaf. An empty tree has depth 0 and a single node has depth 1.
Trees are written in level order: values are listed level by level, left to right, and null marks a missing child (trailing nulls are omitted).
Example 1
- Input:
- root = [1,2,3,null,4,5,null,null,6]
- Output:
- 4
- Explanation
- The longest root-to-leaf path is 1 → 2 → 4 → 6, which has 4 nodes. The other branch, 1 → 3 → 5, only has 3.
Example 2
- Input:
- root = [1,2,null,3]
- Output:
- 3
- Explanation
- Three nodes in a chain leaning left give a depth of 3; depth counts nodes, not edges.
Example 3
- Input:
- root = [10,20,30,40,50,60,70]
- Output:
- 3
- Explanation
- A perfect tree with three full levels has depth 3.
Constraints
- The number of nodes is in the range
[0, 10^4] -100 <= Node.val <= 100- The tree height is at most
500