Watch 10 video solutions for Maximum Depth of N-ary Tree, a easy level problem involving Tree, Depth-First Search, Breadth-First Search. This walkthrough by Nick White has 27,711 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given a n-ary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).
Example 1:

Input: root = [1,null,3,2,4,null,5,6] Output: 3
Example 2:

Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14] Output: 5
Constraints:
[0, 104].1000.Problem Overview: You receive the root of an N-ary tree where each node can have multiple children. The task is to compute the maximum depth of the tree, defined as the number of nodes along the longest path from the root node down to the farthest leaf node.
Approach 1: Recursive Depth-First Search (DFS) (Time: O(n), Space: O(h))
The recursive DFS approach walks down each branch of the tree and tracks the deepest path encountered. Starting from the root, recursively compute the depth of every child node using a function call like depth(child). The depth of the current node equals 1 + max(child depths). If a node has no children, its depth is 1. This approach works naturally with trees because recursion mirrors the hierarchical structure. Time complexity is O(n) since each node is visited exactly once. Space complexity is O(h), where h is the height of the tree, due to the recursion stack. This method is the most concise and is commonly used when working with Tree recursion patterns.
Approach 2: Iterative Breadth-First Search (BFS) (Time: O(n), Space: O(w))
The BFS approach computes depth level by level using a queue. Start by pushing the root node into the queue. Each iteration processes all nodes at the current level, then pushes their children into the queue for the next level. After finishing one level, increment a depth counter. Once the queue becomes empty, the counter represents the maximum depth. This technique is a classic Breadth-First Search level-order traversal. Time complexity remains O(n) because every node enters and leaves the queue once. Space complexity is O(w), where w is the maximum number of nodes at any level (tree width). BFS is useful when you want explicit level tracking or when recursion depth might become large.
Both methods traverse the entire tree and produce the same result. DFS focuses on exploring deep paths first, while BFS expands outward level by level. These patterns appear frequently in Depth-First Search and tree traversal interview questions.
Recommended for interviews: Recursive DFS is usually the expected solution because it is short, intuitive, and directly expresses the definition of tree depth. BFS demonstrates strong understanding of level-order traversal and queue-based tree processing. Showing DFS first and mentioning BFS as an alternative signals solid tree traversal knowledge.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recursive Depth-First Search (DFS) | O(n) | O(h) | Preferred for interviews and typical tree recursion problems |
| Iterative Breadth-First Search (BFS) | O(n) | O(w) | Useful when processing trees level-by-level or avoiding deep recursion |