一直觉得二叉树的后续遍历非常绕,所以这里就结合自己的感受来阐述一下我的理解
![7715645f59333828319f0d683fdedc08.png](https://i-blog.csdnimg.cn/blog_migrate/65118dec676eb95a3fcf1dd77b6b69c7.jpeg)
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)