class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
if not root:
return []
ans = list()
q = deque([root])
while q:
cnt = len(q)
level = list()
for _ in range(cnt):
cur = q.popleft()
level.append(cur.val)
for child in cur.children:
q.append(child)
ans.append(level)
return ans
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/n-ary-tree-level-order-traversal/solution/n-cha-shu-de-ceng-xu-bian-li-by-leetcode-lxdr/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。