All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : July 04, 2024
Last updated : July 04, 2024
Related Topics : Tree, Breadth-First Search
Acceptance Rate : 71.03 %
"""
# 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