2Brute2Best
🔥0

N-ary Tree Level Order Traversal

Medium·LeetCode #429 Pro OnlySlight Variation
LeetCode
01

Problem Statement

You are given the root of an N-ary tree. In this tree, each node can have any number of children, not just two. Return the node values one row at a time, from the top down. Inside each row, read the nodes from left to right.

Each row becomes one inner list in the answer. An empty tree gives an empty answer.

Input

The root of an N-ary tree. Each node has a value and a children list. It can be empty (null).

In the examples, the tree is written in LeetCode's level-order form. Write the root, then a null. After that, for each node in level order, write its children (left to right), then a null. The null after a node's children is a group separator, not a missing child. Trailing nulls are dropped.

So [1, null, 3, 2, 4, null, 5, 6] means: the root is 1 and its children are 3, 2, 4. Then the children of 3 are 5 and 6. Nodes 2 and 4 have no children.

Output

A list of rows. The first list is the root's row, the next list holds the children, and so on. Each row is read left to right.

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

Row 0 is 1. Row 1 is its children 3, 2 and 4. Row 2 is 5 and 6, the children of 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: [[1], [2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13], [14]]

Each group of children is separated by a null. Node 2 has no children, so its group is empty and it is only a null. Node 3 has children 6 and 7.

Example 3
Input: root = []
Output: []

No nodes, so no rows.

Constraints
  • • The height of the n-ary tree is less than or equal to 1000
  • • The total number of nodes is between [0, 10^4]
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“N-ary tree”
→ A node has a list of children, not two fixed slots. The loop over children replaces the left and right checks.
“level order”
→ Rows are depths, so this is still a breadth-first question.
“null in the input”
→ A group separator, not a missing node. It only matters when reading the input.
03

Approach Complexity Ladder

Brute Force — walk the whole tree once per rowO(N · H)O(H)

For each depth d, walk the whole tree through every child and keep the nodes at depth d

Optimal — BFS over children with a row-size snapshotO(N)O(N)

Queue BFS; take size = q.size() each round, pop that many, push every child

Brute Force Intuition

Find the height H. For each depth d from 0 to H − 1, walk the whole tree, going through every child list, and collect the nodes whose depth is exactly d. Each walk gives one row.

Recognition rule for this sub-pattern:

Pop exactly `size` nodes per round; that round is one level.
Remember this · Tree BFS
Trigger
Level order, per-level max/average, right side view, minimum depth, nodes at distance k.
Think
Does the question care about depth / rows rather than paths?
Key question
How many nodes are in the current level (queue size at the start of the level)?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Tree BFS translate into production architectures and low-level memory mechanics.