> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Clone N-ary Tree Python Solution with Tests

> Tested Python solution for LeetCode 1490 with 21 pytest cases. Generate a practice environment with lcpy.

LeetCode 1490, [Medium](/catalog/medium). Topics: [Tree](/catalog/topics/tree), [Depth-First Search](/catalog/topics/depth-first-search), [Breadth-First Search](/catalog/topics/breadth-first-search), [Hash Table](/catalog/topics/hash-table). [View on LeetCode](https://leetcode.com/problems/clone-n-ary-tree/description/).

Generate this problem as a practice environment: tested reference solution, 21 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1490   # by problem number
lcpy gen -s clone_n_ary_tree   # by problem name
```

## Problem

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).*

### Examples

![Example 1](https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png)

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

![Example 2](https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png)

```
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

* The depth of the n-ary tree is less than or equal to 1000.
* The total number of nodes is between \[0, 10^4].

**Follow up:** Can your solution work for the graph problem?

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/clone_n_ary_tree/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/clone_n_ary_tree/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from __future__ import annotations

from collections import deque


class NaryNode:
    def __init__(self, val: int = 0, children: list[NaryNode] | None = None) -> None:
        self.val = val
        self.children = children if children is not None else []


class Solution:
    # Time: O(n)
    # Space: O(n)
    def clone_tree(self, root: NaryNode | None) -> NaryNode | None:
        if root is None:
            return None
        clones: dict[NaryNode, NaryNode] = {root: NaryNode(root.val)}
        queue: deque[NaryNode] = deque([root])
        while queue:
            node = queue.popleft()
            for child in node.children:
                clones[child] = NaryNode(child.val)
                clones[node].children.append(clones[child])
                queue.append(child)
        return clones[root]
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.