Maximum Depth of Binary Tree — Trees
Count the levels in a hierarchy, measuring from the head of the structure to its deepest member; an empty one has no levels.
Trees · Easy
The problem
How many levels deep does a company org chart go, counting from the top? The depth of a binary tree is the number of nodes on its longest downward path from the root to a leaf. Return the depth of the given tree.
The story
An HR team wants to know how many layers their org chart has. The chart is a tree of up to 1500 people, where each person has at most two direct reports. Count the people on the longest chain from the top down to someone with no reports. An empty chart has depth 0, and one person alone has depth 1.
What to return
- Return a single integer: how many nodes lie on the longest root-to-leaf path.
- An empty tree has depth 0; a single node has depth 1.
Constraints
- 0 <= number of nodes <= 1500
- -20 <= node value <= 20
- Your code may run at most 42,020 counted steps on the largest case.
Example
- Input
root = [8,4,12,null,6,9,15,null,null,null,7]- Output
4