easyTreesPure DSA~15 min
Navigation Drawer Depth
A mobile app's navigation drawer is a tree of menu items. To pick the right animation duration, the renderer needs the maximum nesting depth of the tree (depth of root is 1).
Problem
Given the root of an n-ary tree, return its maximum depth — the number of nodes on the longest path from root to any leaf. The root alone counts as depth 1; an empty tree has depth 0.
Input
Root node of an n-ary tree with 0 ≤ n ≤ 10^4 nodes. Each node has an integer value and a list of zero or more children.
Output
A single integer — the maximum depth.
Constraints
- 0 ≤ n ≤ 10,000
- Tree is well-formed (no cycles, every non-root has exactly one parent)
- Recursion depth may approach 10^4 in pathological cases
Examples
Example 1
Input
Root with two children, each with one child of their own
Output
3
root → child → grandchild = 3 nodes.
Example 2
Input
Empty tree (root = null)
Output
0
No nodes means depth zero.