Imagine you need to create an exact duplicate of a complex organizational chart or family tree structure. That's exactly what we're doing here - creating a deep copy of an N-ary tree!

Given the root of an N-ary tree, your task is to return a complete clone of the entire tree structure. Each node in the tree contains:

  • An integer value (val)
  • A list of child nodes (children)

The clone must be completely independent - modifying the original tree should not affect the cloned tree and vice versa. Think of it as creating a photocopy of a document where changes to the original don't affect the copy.

Key Challenge: Unlike binary trees which have at most 2 children, N-ary trees can have any number of children, making the cloning process more complex as we need to handle variable-length child lists.

Input & Output

example_1.py β€” Basic N-ary Tree
$ Input: root = [1,null,3,2,4,null,5,6]
β€Ί Output: Cloned tree with same structure: [1,null,3,2,4,null,5,6]
πŸ’‘ Note: The tree has root with value 1 and three children (3,2,4). Node 3 has two children (5,6). The clone maintains the exact same structure but creates completely new node objects.
example_2.py β€” Single Node Tree
$ Input: root = [1]
β€Ί Output: Cloned single node: [1]
πŸ’‘ Note: A tree with only one node (the root) is cloned to create a new node with the same value but different memory address.
example_3.py β€” Empty Tree
$ Input: root = null
β€Ί Output: null
πŸ’‘ Note: An empty tree (null root) returns null as there's nothing to clone. This is the base case for our recursive solution.

Visualization

Tap to expand
Tree Cloning Process: Original β†’ CloneOriginal Tree1324567Clone ProcessStep 1:CreateCloned Tree1324567Cloning Steps:1. Start at Root:Begin cloning process at root node (value 1)2. Create Clone Node:Create new node with same value, different memory address3. Process Children Recursively:For each child, repeat the cloning process (DFS traversal)4. Maintain Relationships:Link cloned children to cloned parents, preserving tree structure
Understanding the Visualization
1
Start at Root
Begin the cloning process at the root of the original tree
2
Create Clone
Create a new node with the same value as the current node
3
Process Children
Recursively clone each child node in the children list
4
Link Structure
Connect cloned children to their cloned parent
5
Complete Tree
Return the root of the completely independent cloned tree
Key Takeaway
🎯 Key Insight: Tree cloning requires creating completely independent node objects while preserving the exact same structural relationships - like making a perfect organizational chart copy where changes to one don't affect the other.

Time & Space Complexity

Time Complexity
⏱️
O(n)

Visit each node exactly once, hash map operations are O(1)

n
2n
βœ“ Linear Growth
Space Complexity
O(n)

Hash map stores n entries plus recursion stack depth

n
2n
⚑ Linearithmic Space

Constraints

  • The number of nodes in the tree is in the range [0, 104]
  • -1000 ≀ Node.val ≀ 1000
  • The depth of the n-ary tree is less than or equal to 1000
  • Follow up: Can you solve this problem iteratively using BFS?
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28 Apple 22
58.0K Views
Medium Frequency
~15 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen