二叉树的层序遍历,利用队列先进先出的性质
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: # 返回从上到下每个节点值列表,例:[1,2,3] def PrintFromTopToBottom(self, root): # write code here if not root:return '' queue = [root] res = [] while queue: if queue[0].left: queue.append(queue[0].left) if queue[0].right: queue.append(queue[0].right) res.append(queue[0].val) queue.pop(0) return res