Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example, given a 3-ary
tree:
We should return its level order traversal:
[ [1], [3,2,4], [5,6] ]
- The depth of the tree is at most
1000
. - The total number of nodes is at most
5000
.
"""
# Definition for a Node.
class Node:
def __init__(self, val, children):
self.val = val
self.children = children
"""
class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
if not root:
return []
ret = []
nodes = [root]
while nodes:
level = []
temp = []
for cur in nodes:
level.append(cur.val)
temp.extend(cur.children)
nodes = temp
ret.append(level)
return ret
# Definition for a Node.
# class Node
# attr_accessor :val, :children
# def initialize(val)
# @val = val
# @children = []
# end
# end
# @param {Node} root
# @return {List[List[int]]}
def levelOrder(root)
return [] if root.nil?
ret = []
nodes = [root]
while not nodes.empty?
level = []
temp = []
for curr in nodes
level.push(curr.val)
temp.concat(curr.children)
end
nodes = temp
ret.push(level)
end
return ret
end