用一个队列存放每一层的节点, 遍历该队列再把下一层的节点存放到队列,
循环终止:队列为空
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): def levelOrder(self, root): """ :type root: TreeNode :rtype: List[int] """ if root is None: return [] r = [root] out=[] while len(r)!=0: b=[] for node in r: out.append(node.val) if node.left is not None: b.append(node.left) if node.right is not None: b.append(node.right) r = b return out