Skip to main content

429 - N-ary Tree Level Order Traversal

Details

KeyValue
Linkhttps://leetcode.com/problems/n-ary-tree-level-order-traversal/
LanguagePython 3
Runtime60 ms, faster than 83.32% of Python3 online submissions for N-ary Tree Level Order Traversal
Memory Usage16.1 MB, less than 50.08% of Python3 online submissions for N-ary Tree Level Order Traversal
DatastructuresNode, deque
AlgorithmsBFS
ComplexityTime: O(N) Memory: O(N)

Procedure

  1. ...

Code

Option 1: Verbose

"""
# 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]]:
if not root: return None
Q, result = deque([root]), []

while Q:
level = []
for _ in range(len(Q)):
node = Q.popleft()
level.append(node.val)
for child in node.children: Q.append(child)

result.append(level)

return result

Option 1: Terse

"""
# 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]]:
if not root: return []
Q, result = deque([root]), []
while Q:
result.append([node.val for node in Q])
Q = [child for node in Q for child in node.children]

return result