Python 版
# -*- 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
if root is None:
return []
tmp = [root]
res = []
while tmp:
n = tmp[0]
res.append(n.val)
if n.left:
tmp.append(n.left)
if n.right:
tmp.append(n.right)
del tmp[0]
return res