二叉树非递归后续遍历
2019-12-24 本文已影响0人
而立之年的技术控
一直觉得二叉树的后续遍历非常绕,所以这里就结合自己的感受来阐述一下我的理解

def lastOrder(root):
if not root:
return None
tmp = root
stack = []
while tmp or stack:
while tmp:
stack.append(tmp)
tmp = tmp.left
node = stack[-1]
tmp = node.right
if tmp is None:
node = stack.pop()
print(node.val)
while stack and node == stack[-1].right:
node = stack.pop()
print(node.val)