【leetcode】429. N-ary Tree Level Order Traversal
题目如下:
解题思路:没啥好说的,多叉树的层序遍历。话说leetcode上面二(多)叉树的前序中序后序层序遍历的题很多,有种凑题数的嫌疑。
代码如下:
class Solution(object): res = [] def traverse(self,node,level): if level > len(self.res): self.res.append([]) self.res[level-1].append(node.val) for i in node.children: if i != None: self.traverse(i,level+1) def levelOrder(self, root): """ :type root: Node :rtype: List[List[int]] """ if root == None: return [] self.res = [] self.traverse(root,1) return self.res