Binary Tree Postorder Traversal (Python)
kitt
posted @ 2014年2月13日 01:43
in LeetCode
, 1372 阅读
二叉树的非递归后序遍历, 还是此文, ^_^
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: # @param root, a tree node # @return a list of integers def postorderTraversal(self, root): stack = []; res = [] pre = None while root or stack: if root: stack.append(root) root = root.left elif pre == stack[-1].right: pre = stack.pop() res.append(pre.val) else: root = stack[-1].right pre = None return res