Skip to content

Latest commit

 

History

History
54 lines (39 loc) · 1.18 KB

_429. N-ary Tree Level Order Traversal.md

File metadata and controls

54 lines (39 loc) · 1.18 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : July 04, 2024

Last updated : July 04, 2024


Related Topics : Tree, Breadth-First Search

Acceptance Rate : 71.03 %


Solutions

Python

"""
# Definition for a Node.
class Node:
    def __init__(self, val=None, children=None):
        self.val = val
        self.children = children
"""

class Solution:
    def levelOrder(self, root: 'Node') -> List[List[int]]:
        output = []

        def dfs(curr: 'Node', depth: int = 0) -> None :
            if not curr :
                return
            if depth >= len(output) :
                output.append([])

            output[depth].append(curr.val)

            depth += 1
            for child in curr.children :
                dfs(child, depth)

        dfs(root)
        return output