Implement according to a zigzag print function binary tree, i.e., the first row from left to right order of printing, the print order of the second row from right to left, the third line from left to right order of printing, a line other analogy.
class TreeNode:
def __init__(self,x):
self.val = x
self.left = None
self.right = None
class Solution:
def Print(self, pRoot):
if pRoot==None:
return []
stack1=[pRoot]
stack2=[]
ret = []
while stack1 or stack2:
if stack1:
tmpRet=[]
while stack1:
tmpNode=stack1.pop()
tmpRet.append(tmpNode.val)
if tmpNode.left:
stack2.append(tmpNode.left)
if tmpNode.right:
stack2.append(tmpNode.right)
ret.append(tmpRet)
if stack2:
tmpRet=[]
while stack2:
tmpNode = stack2.pop()
tmpRet.append(tmpNode)
if tmpNode.right:
stack1.append(tmpNode.right)
if tmpNode.left:
stack1.append(tmpNode.left)
ret.append(tmpRet.val)
return ret