Watch 7 video solutions for Clone N-ary Tree, a medium level problem involving Hash Table, Tree, Depth-First Search. This walkthrough by Fisher Coder has 1,068 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given a root of an N-ary tree, return a deep copy (clone) of the tree.
Each node in the n-ary tree contains a val (int) and a list (List[Node]) of its children.
class Node {
public int val;
public List<Node> children;
}
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: [1,null,3,2,4,null,5,6]
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: [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]
Constraints:
1000.[0, 104].Follow up: Can your solution work for the graph problem?
Problem Overview: You receive the root of an N-ary tree and must return a deep copy of it. Every node in the new tree must be newly created, but the structure and values must match the original tree exactly.
An N-ary tree node contains a value and a list of children. Cloning requires recreating each node and rebuilding the children relationships. The key rule: never reuse existing nodes from the original tree. Each node must be duplicated.
Approach 1: Depth-First Search Clone (DFS) (Time: O(n), Space: O(n))
The most common solution performs a recursive depth-first search. For each node you visit, create a new node with the same value. Then recursively clone every child and append the returned clones to the new node’s children list. Because an N-ary tree has no cycles, you do not need a visited set or hash table. Each node is processed exactly once. The recursion stack may grow up to the tree height, giving O(h) stack usage, which in the worst case becomes O(n).
The key insight: cloning naturally mirrors traversal. When DFS reaches a node, construct its copy immediately and recursively build its subtree. The returned cloned children automatically rebuild the structure.
Approach 2: Breadth-First Search Clone (BFS) (Time: O(n), Space: O(n))
You can also clone the tree using breadth-first search. Start by creating a clone of the root and pushing the pair (original, clone) into a queue. For every node dequeued, iterate through its children, create cloned child nodes, attach them to the cloned parent, and enqueue the pair for further processing. BFS processes the tree level by level and constructs the same hierarchy in the new tree.
This approach avoids recursion and uses an explicit queue. Space usage comes from the queue storing nodes from the current level. The cloning logic is still straightforward: each original node produces exactly one cloned node.
Recommended for interviews: The recursive DFS clone is what most interviewers expect. It is concise, clearly mirrors the tree structure, and demonstrates strong understanding of tree traversal. BFS works equally well and is useful when recursion depth could be large. Mentioning both approaches shows deeper understanding of traversal strategies.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Depth-First Search (Recursive Clone) | O(n) | O(n) | Most common interview solution. Clean and directly mirrors tree structure. |
| Breadth-First Search (Queue) | O(n) | O(n) | When avoiding recursion or handling very deep trees. |