↓Skip to main content
  1. LeetCode/

LeetCode 429: N-ary Tree Level Order Traversal

·1 min· ·
LeetCode Medium Tree Breadth-First-Search
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-10-24

  • Submissions: Accepted (first try)

Approach
#

Same as level order on a binary tree (Tree Template 2), except the children are a children list instead of left and right. Record the queue length at the start of each round; the nodes popped in that round form one level.

My Attempts
#

Accepted on the first try.

Solution
#

class Solution:
    def levelOrder(self, root: 'Node') -> List[List[int]]:
        if not root:
            return []
        q = deque([root])
        res = []
        while q:
            level = []
            for _ in range(len(q)):
                node = q.popleft()
                level.append(node.val)
                q.extend(node.children)
            res.append(level)
        return res

Complexity
#

Time O(n), space O(w) for the widest level.

Mistakes to Avoid
#

  • Return [] for an empty tree.
  • Take the level size before the loop; the queue keeps growing inside it.

Interview Explanation
#

This is BFS level order. Each round I record the queue length and pop that many nodes as one level, pushing every node’s children. Time O(n), space proportional to the widest level.

Related

LeetCode 515: Find Largest Value in Each Tree Row
·1 min
LeetCode Medium Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 515 note: BFS level order, taking the maximum of each level; start from -inf
LeetCode 314: Binary Tree Vertical Order Traversal
·2 mins
LeetCode Medium Hash-Table Tree Depth-First-Search Breadth-First-Search Sorting Binary-Tree
LeetCode 314 note: BFS with a column index; BFS is what guarantees top-to-bottom within a column and left-to-right within a row
LeetCode 3787: Find Diameter Endpoints of a Tree
·2 mins
LeetCode Medium Tree Breadth-First-Search Graph
LeetCode 3787 note: two BFS passes find one diameter’s ends A and B; every node’s farthest node is A or B, so a node is a diameter endpoint exactly when max(dist(A,i), dist(B,i)) equals the diameter
LeetCode 116: Populating Next Right Pointers in Each Node
·2 mins
LeetCode Medium Linked-List Tree Depth-First-Search Breadth-First-Search
LeetCode 116 note: in a perfect binary tree, walk each level through the next pointers already set on the level above: left.next = right and right.next = next.left, with O(1) extra space
LeetCode 117: Populating Next Right Pointers in Each Node II
·2 mins
LeetCode Medium Linked-List Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 117 note: connect next pointers level by level with BFS; for a non-perfect tree, looking only at the neighbour’s children breaks the chain
LeetCode 102: Binary Tree Level Order Traversal
·2 mins
LeetCode Medium Tree Breadth-First-Search Binary-Tree
LeetCode 102 note: BFS one whole level at a time, building the next level from the current one; an empty tree returns an empty list