Binary Tree zigzag print order
Title Description
Please implement a function according to a binary tree zigzag print, i.e., the first row from left to right order of printing, the print order of the second layer is from right to left, the third row from left to right order of printing, other line and so on.
# -*- 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
resultarray = []
if not pRoot:
return resultarray
currnodes = [pRoot]
flag = True
while currnodes:
currvalues = []
nextnodes = []
for node in currnodes:
currvalues.append(node.val)
if node.left:
nextnodes.append(node.left)
if node.right:
nextnodes.append(node.right)
currnodes = nextnodes
resultarray.append(currvalues) if flag else resultarray.append(currvalues[::-1])
flag = not flag
return resultarray