标签:pre coding 打印二叉树 color ini sel 节点 title app
1 # -*- coding:utf-8 -*-
2 # class TreeNode:
3 # def __init__(self, x):
4 # self.val = x
5 # self.left = None
6 # self.right = None
7 class Solution:
8 # 返回二维列表[[1,2],[4,5]]
9 def Print(self, pRoot):
10 # write code here
11 result = []
12 cur_tmp = []
13 if pRoot==None:
14 return result
15 cur_tmp.append(pRoot)
16 result.append([t.val for t in cur_tmp])
17
18 while cur_tmp:
19 next_tmp= []
20 for i in cur_tmp:
21 if i.left:
22 next_tmp.append(i.left)
23 if i.right:
24 next_tmp.append(i.right)
25 if next_tmp: # 最后一层的叶子节点,next_tmp是空的
26 result.append([t.val for t in next_tmp])
27 cur_tmp = next_tmp
28
29 return result
标签:pre coding 打印二叉树 color ini sel 节点 title app
原文地址:https://www.cnblogs.com/shuangcao/p/13368616.html