题解 | #求二叉树的层序遍历#
求二叉树的层序遍历
http://www.nowcoder.com/practice/04a5560e43e24e9db4595865dc9c63a3
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # # @param root TreeNode类 # @return int整型二维数组 # class Solution: def levelOrder(self , root ): def do(root ,level): if root == None: return else: if len(self.treelist) < level: self.treelist.append([]) self.treelist[level-1].append(root.val) if root.left != None: do(root.left, level+1) if root.right != None: do(root.right, level+1) # write code here self.treelist = [] do(root,1) return self.treelist