题目描述:
方法一:递归
class Solution: def postorderTraversal(self, root: TreeNode) -> List[int]: res = [] def helper(root): if not root: return helper(root.left) helper(root.right) res.append(root.val) helper(root) return res
方法二:迭代
class Solution: def postorderTraversal(self, root: TreeNode) -> List[int]: res = [] if not root: return res stack = [root] while stack: node = stack.pop() if node.left: stack.append(node.left) if node.right: stack.append(node.right) res.append(node.val) return res[::-1]
原文:https://www.cnblogs.com/oldby/p/11181844.html