题解 | #按之字形顺序打印二叉树#
按之字形顺序打印二叉树
http://www.nowcoder.com/practice/91b69814117f4e8097390d107d2efbe0
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param pRoot TreeNode类
# @return int整型二维数组
#
class Solution:
def Print(self , pRoot: TreeNode) -> List[List[int]]:
res = []
if not pRoot:
return res
quene = []
count = 0
quene.append(pRoot)
while quene:
level = []
size = len(quene)
for _ in range(size):
cur = quene.pop(0)
level.append(cur.val)
if cur.left:
quene.append(cur.left)
if cur.right:
quene.append(cur.right)
count += 1
if count % 2 == 0:
res.append(level[::-1])
else:
res.append(level)
return res