class Solution:
def Print(self, pRoot):
# write code here
if not pRoot:
return []
res = []
s = [pRoot]
left_to_right=True
while len(s)>0:
curValues = []
nextLevel = []
for i in s:
curValues.append(i.val)
if i.left:
nextLevel.append(i.left)
if i.right:
nextLevel.append(i.right)
if not left_to_right:
curValues.reverse()
if curValues:
res.append(curValues)
s=nextLevel
left_to_right = not left_to_right
return res
请实现一个函数按照之字形打印二叉树
最新推荐文章于 2021-01-16 23:20:59 发布