-- coding:utf-8 --

class TreeNode:

def init(self, x):

self.val = x

self.left = None

self.right = None

class Solution:
# 返回从上到下每个节点值列表,例:[1,2,3]
current_layer=-1
list_val=[]
list_layer=[]
def PrintFromTopToBottom(self, root):
# write code here
self.FromTopToBottom(root)
if len(self.list_val)!=0:
return self.cal_ans(self.list_val,self.list_layer)
else:
return None
def FromTopToBottom(self, root):
self.current_layer+=1
if root==None:
self.current_layer-=1
return None
else:
self.list_val.append(root.val)
self.list_layer.append(self.current_layer)
self.FromTopToBottom(root.left)
self.FromTopToBottom(root.right)
self.current_layer-=1
return
#return self.cal_ans(self.list_val,self.list_layer)
def cal_ans(self,list_val,list_layer):
layer_num=max(list_layer)+1
ans=[]
for i in range(layer_num):
ans.append([])
for i in range(len(list_val)):
ans[list_layer[i]].append(list_val[i])
output=[]
for i in range(layer_num):
output=output+ans[i]
return output