Maximum Depth of Binary Tree
Given the root of a binary tree, return its maximum depth: the number of nodes along the longest path from the root down to the farthest leaf node.
Open official problem prompt ↗Measure how many levels the tree has — the length in nodes of the longest chain from the root to any leaf.
Like measuring the height of a family tree by asking each person 'how many generations are below you?', then taking 1 plus the deepest branch.
- Input
- root = [3, 9, 20, null, null, 15, 7]
- Output
- 3
- Why
- The longest root-to-leaf path is 3 -> 20 -> 15 (or 3 -> 20 -> 7), which visits 3 nodes.
The number of nodes is in the range [0, 10^4]-100 <= Node.val <= 100