> ## 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.

# N-ary Tree Level Order Traversal

> Tested Python solution for LeetCode 429 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 429, [Medium](/catalog/medium). Topics: [Tree](/catalog/topics/tree), [Breadth-First Search](/catalog/topics/breadth-first-search). [View on LeetCode](https://leetcode.com/problems/n-ary-tree-level-order-traversal/description/).

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

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

## Problem

Given an n-ary tree, return *the level order traversal of its nodes' values*.

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],[3,2,4],[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],[2,3,4,5],[6,7,8,9,10],[11,12,13],[14]]
```

### Constraints

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/n_ary_tree_level_order_traversal/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/n_ary_tree_level_order_traversal/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(w)
    def level_order(self, root: NaryNode | None) -> list[list[int]]:
        levels: list[list[int]] = []
        if root is None:
            return levels
        current: deque[NaryNode] = deque([root])
        while current:
            levels.append([node.val for node in current])
            nxt: deque[NaryNode] = deque()
            for node in current:
                nxt.extend(node.children)
            current = nxt
        return levels
```

## Complexity

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

## Tags


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