NeetCode #384LC-145EasyTreesNC 250
← Back to All Problems#384 · #145 · Binary Tree Postorder Traversal(二叉树的后序遍历)
📌 Problem Statement & Constraints
Given the root of a binary tree, return its post-order traversal (left, right, root) as a list of values. Constraints: the number of nodes is in
[0, 100]. The follow-up asks for an iterative solution.💡 Core Algorithmic Approaches
- A neat trick: a modified pre-order that visits root, right, left produces the reverse of the post-order sequence.
- So push right before left (the opposite of pre-order), collect the values, then reverse the result.
- This avoids the fiddly state machine needed for a direct iterative post-order traversal.
- An alternative marks visited nodes with a second stack or a
(node, visited)pair.
💻 Benchmark Python3 Implementation
class Solution:
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
if not root:
return []
res = []
st = [root]
while st:
node = st.pop()
res.append(node.val)
# push left first so right is processed first -> root, right, left
if node.left:
st.append(node.left)
if node.right:
st.append(node.right)
return res[::-1] # reverse -> left, right, root⚡ Complexity Deep Dive
⏱️ Time Complexity
O(n): each node is pushed and popped once, plus one reversal.
💾 Space Complexity
O(n) for the stack and the result.
⚠️ Interview Pitfalls & Follow-ups
- Pushing in pre-order order and forgetting the reversal: the output would be root, right, left.
- Trying to do a direct iterative post-order with one stack: it requires tracking whether the right subtree has been visited, which is easy to get wrong.
- Reversing the tree instead of the output: the traversal order is what matters.
- Recursing on a deep tree: the recursion limit can be exceeded.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🧩 Related Trees Problems
中文速览与面试要点
二叉树的后序遍历
给你一棵二叉树的根节点 root,返回其节点值的 后序遍历 。进阶:递归算法很简单,你可以通过迭代算法完成吗?约束:树中节点的数目在范围 [0, 100] 内;-100 ≤ Node.val ≤ 100。
核心要点
- 递归(最直观) : 左子树 → 右子树 → 根 。
- 迭代 · 变形前序(巧妙解法) :后序是「左 → 右 → 根」,若把前序改成「根 → 右 → 左」再 整体反转 ,恰好得到后序。因此可以用前序的框架(压栈顺序改为先压左、后压右),最后 res[::-1] 。
- 迭代 · 标记法 :用 (node, visited) 元组入栈, visited 为 False 时表示「尚未处理子节点」,此时压入 (node, True) 与右、左子节点; visited 为 True 时才真正访问。
- 复杂度 :O(n) 时间、O(h) 空间。
常见陷阱
- 直接套用前序的压栈顺序 :会得到「根 → 右 → 左」,反转后是「左 → 右 → 根」——恰好正确!但若压栈顺序写错(先压右再压左)则会得到错误结果。注意本解的压栈顺序是 先左后右 。
- 反转后忘记 res[::-1] :会得到「根 → 右 → 左」的错误顺序。
- 标记法中忘记压入 (node, True) :会导致节点永远不被访问。
参考实现
class Solution:
def postorderTraversal(self, root: 'TreeNode') -> list[int]:
# 变形前序 + 反转
if not root:
return []
res = []
stack = [root]
while stack:
node = stack.pop()
res.append(node.val)
# 先压左、再压右(与前序相反)
if node.left:
stack.append(node.left)
if node.right:
stack.append(node.right)
return res[::-1] # 反转得到后序
# 递归版本
# def postorderTraversal(self, root):
# if not root: return []
# return (self.postorderTraversal(root.left)
# + self.postorderTraversal(root.right)
# + [root.val])
# 标记法(不反转)
# def postorderTraversal(self, root):
# res, stack = [], [(root, False)] if root else []
# while stack:
# node, visited = stack.pop()
# if visited:
# res.append(node.val)
# else:
# stack.append((node, True))
# if node.right: stack.append((node.right, False))
# if node.left: stack.append((node.left, False))
# return res