题解 | #从上往下打印二叉树#
从上往下打印二叉树
http://www.nowcoder.com/practice/7fe2212963db4790b57431d9ed259701
# -*- coding:utf-8 -*-
# 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
que = []
ret = []
if root is None:
return
que.append(root)
while que:
que_tmp = []
for item in que:
ret.append(item.val)
# print(item.val)
if item.left:
que_tmp.append(item.left)
if item.right:
que_tmp.append(item.right)
que = que_tmp
# print(que)
return ret