N-ary Tree

Maximum Depth of N-ary Tree

Easy
Solve it on LeetCode ↗

The problem

Return the maximum depth (number of nodes on the longest root-to-leaf path) of an n-ary tree, where each node has a list of children.

Stuck? Reveal hints one at a time

How to approach it

  1. 1Base: null node → 0.
  2. 2Recurse into every child, taking the maximum child depth (0 if no children).
  3. 3Return 1 + that maximum.

Key insight

Binary-tree recursions generalize to n-ary by replacing the left/right pair with a loop (or max/map) over children.

The solution

Watch out for

  • max() over an empty children list needs a default (leaf nodes).
  • BFS counting levels works identically if recursion depth is a concern.