Skip to content
LC-0429 Medium LeetCode

429. N-ary Tree Level Order Traversal

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 71% Topics: Tree, Breadth-First Search
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(w)

class Node(object):
    def __init__(self, val, children):
        self.val = val
        self.children = children


class Solution(object):
    def levelOrder(self, root):
        """
        :type root: Node
        :rtype: List[List[int]]
        """
        if not root:
            return []
        result, q = [], [root]
        while q:
            result.append([node.val for node in q])
            q = [child for node in q for child in node.children if child]
        return result

Solution from kamyu104/LeetCode-Solutions · MIT