tempList =[] while stack1: top = stack1.pop() tempList.append(top.val) if top.left: stack2.append(top.left) if top.right: stack2.append(top.right) ans.append(tempList) tempList =[] while stack2: top = stack2.pop() tempList.append(top.val) if top.right: stack1.append(top.right) if top.left: stack1.append(top.left) if tempList: ans.append(tempList) return ans if __name__ == '__main__': so = Solution() root = TreeNode.buildBinaryTreeFromSer([1,2,3,4,5,6]) print(so.zigzagLevelOrder2stack(root))
stack1.append(root) while stack1: tempList = [] while stack1: top = stack1.pop() tempList.append(top.val) if top.left: stack2.append(top.left) if top.right: stack2.append(top.right) ans.append(tempList) tempList = [] while stack2: top = stack2.pop() tempList.append(top.val) if top.right: stack1.append(top.right) if top.left: stack1.append(top.left) if tempList: ans.append(tempList) return ans if __name__ == '__main__': so = Solution() root = TreeNode.buildBinaryTreeFromSer([1, 2, 3, 4, 5, 6]) print(so.zigzagLevelOrder2stack(root))