题解 | #按之字形顺序打印二叉树#
按之字形顺序打印二叉树
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]]:
if pRoot==None:return None
res,ceng_stack=[],[pRoot]
a=1
while ceng_stack:
ceng_nodes=[]
for i in range(len(ceng_stack)):
node=ceng_stack.pop(0)
ceng_nodes.append(node.val)
if node.left:ceng_stack.append(node.left)
if node.right:ceng_stack.append(node.right)
if a%2!=0:res.append(ceng_nodes)
else:res.append(ceng_nodes[::-1])
a=a+1
return res