easyTreesPure DSA~15 min
Org Chart Depth
An HR tool renders an org chart as a binary tree of reporting lines and wants to size the canvas before drawing. Compute the maximum depth — the number of nodes on the longest path from the CEO (root) down to any leaf employee.
Problem
Given the root of a binary tree, return its maximum depth: the number of nodes along the longest root-to-leaf path.
Input
The root of a binary tree with n nodes (0 ≤ n ≤ 10^5). An empty tree has root = null.
Output
A single integer — the maximum depth (0 for an empty tree).
Constraints
- 0 ≤ n ≤ 100,000
- Each node has at most two children
- Depth counts nodes, not edges
Examples
Example 1
Input
tree = [3, 9, 20, null, null, 15, 7]
Output
3
Root 3 → 20 → 15 (or 7) is the longest path, with 3 nodes.
Example 2
Input
tree = []
Output
0
An empty tree has depth 0.