一直觉得二叉树的后续遍历非常绕,所以这里就结合自己的感受来阐述一下我的理解
![](https://img.haomeiwen.com/i13792534/fe3372d3e3b08e40.jpg)
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)
网友评论