N-ary Tree Level Order Traversal - Problem
N-ary Tree Level Order Traversal challenges you to traverse a tree with potentially many children per node, level by level from left to right.

Given an n-ary tree (where each node can have any number of children), return the level order traversal of its nodes' values. This means you should visit all nodes at depth 0, then all nodes at depth 1, then depth 2, and so on.

For example, if you have a tree where the root has value 1 and three children with values [3, 2, 4], and node 3 has children [5, 6], your output should be [[1], [3, 2, 4], [5, 6]] - each inner array represents one level of the tree.

The key insight is that unlike binary trees, each node can have any number of children, making this a perfect application of breadth-first search (BFS).

Input & Output

example_1.py โ€” Basic N-ary Tree
$ Input: root = [1,null,3,2,4,null,5,6]
โ€บ Output: [[1],[3,2,4],[5,6]]
๐Ÿ’ก Note: The tree has root 1 with children [3,2,4]. Node 3 has children [5,6]. Level order gives us: Level 0: [1], Level 1: [3,2,4], Level 2: [5,6]
example_2.py โ€” Deeper Tree
$ 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]]
๐Ÿ’ก Note: A more complex tree with 5 levels. Each level is processed in order from left to right, demonstrating the breadth-first nature of level order traversal.
example_3.py โ€” Single Node
$ Input: root = [1]
โ€บ Output: [[1]]
๐Ÿ’ก Note: Edge case with only root node. The result is a single level containing just the root value.

Constraints

  • The height of the n-ary tree is less than or equal to 1000
  • The total number of nodes is between [0, 104]
  • Node values are between [-100, 100]
  • Follow-up: Recursive solution is trivial, could you do it iteratively?

Visualization

Tap to expand
Office Building Survey (BFS Analogy)Floor 3: CEO (1)Manager(3)Manager(2)Manager(4)Floor 2: Managers [3, 2, 4]56Floor 1: Employees [5, 6]Survey Process (Queue)Step 1: Queue = [CEO] โ†’ Survey CEOStep 2: Queue = [Mgr3,Mgr2,Mgr4] โ†’ Survey allStep 3: Queue = [Emp5,Emp6] โ†’ Survey allStep 4: Queue = [] โ†’ Complete!Survey Results by FloorFloor 3: [1] โ† CEO onlyFloor 2: [3, 2, 4] โ† All managersFloor 1: [5, 6] โ† All employeesFinal Result: [[1], [3,2,4], [5,6]]
Understanding the Visualization
1
Start at Root
Place the CEO (root) in your queue - they're the only person on the top floor
2
Process Current Floor
Interview everyone currently in your queue (one complete floor)
3
Add Next Floor
Each person tells you about their direct reports - add all these to the queue
4
Repeat Floors
Continue until you've surveyed every floor in the building
Key Takeaway
๐ŸŽฏ Key Insight: BFS with a queue naturally processes nodes level-by-level by keeping track of how many nodes belong to the current level, ensuring perfect level-order grouping!
Asked in
Google 42 Amazon 38 Meta 25 Microsoft 31
89.2K Views
High Frequency
~15 min Avg. Time
2.8K 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