题目

分析
bfs遍历树,同时当count%2等于0时正向打印,count%2等于1时逆向打印
python代码
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def Print(self, pRoot):
# write code here
if not pRoot: return []
res = []
arr = [pRoot]
count = 0
while (len(arr)!=0):
temp_arr = []
temp_res = []
for n in arr:
if n:
temp_res.append(n.val)
if n.left:
temp_arr.append(n.left)
if n.right:
temp_arr.append(n.right)
if count%2==0:
res.append(temp_res)
else:
res.append(temp_res[::-1])
arr = temp_arr
count+=1
return res