NeetCode #438LC-106MediumTrees
← Back to All Problems#438 · #106 · Construct Binary Tree from Inorder and Postorder Traversal(从中序与后序遍历序列构造二叉树)
📌 Problem Statement & Constraints
Given two integer arrays
inorder and postorder representing the in-order and post-order traversals of a binary tree, construct and return the tree. All values are distinct. Constraints: 1 <= inorder.length <= 3000, inorder.length == postorder.length.💡 Core Algorithmic Approaches
- The last element of the post-order traversal is the root (mirroring the pre-order case, where it is the first).
- In the in-order array, the root splits the tree into left and right subtrees; the left subtree size tells you how to split the post-order array.
- Precompute a value-to-index map for the in-order array to make the split lookup O(1).
- The recursion builds the left subtree first, then the right, using the correct index ranges.
💻 Benchmark Python3 Implementation
class Solution:
def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
idx = {v: i for i, v in enumerate(inorder)} # O(1) split lookup
def build(il: int, ir: int, pl: int, pr: int) -> Optional[TreeNode]:
if il > ir:
return None
root = TreeNode(postorder[pr]) # post-order's last is the root
m = idx[root.val]
left_size = m - il
root.left = build(il, m - 1, pl, pl + left_size - 1)
root.right = build(m + 1, ir, pl + left_size, pr - 1)
return root
return build(0, len(inorder) - 1, 0, len(postorder) - 1)⚡ Complexity Deep Dive
⏱️ Time Complexity
O(n): each node is created once and each split lookup is O(1).
💾 Space Complexity
O(n) for the index map plus O(h) for the recursion.
⚠️ Interview Pitfalls & Follow-ups
- Taking
postorder[pl]as the root: in a post-order traversal the root is last, atpostorder[pr]. - Slicing the arrays: it works but allocates O(n log n) in total.
- Using
inorder.index(root.val): O(n) per node, giving O(n^2). - Mixing up the post-order bounds: the left subtree occupies
pltopl + left_size - 1, and the rightpl + left_sizetopr - 1.
⚡
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
中文速览与面试要点
从中序与后序遍历序列构造二叉树
给定两个整数数组 inorder 和 postorder,其中 inorder 是二叉树的中序遍历,postorder 是同一棵树的后序遍历,请你构造并返回这颗二叉树。约束:1 ≤ inorder.length ≤ 3000;postorder.length == inorder.length;-3000 ≤ inorder[i], postorder[i] ≤ 3000;inorder 和 postorder 都由 不同的 值组成;postorder 均出现在 inorder;inorder 均出现在 postorder;inorder 保证为二叉树的中序遍历序列;postorder 保证为二叉树的后序遍历序列。
核心要点
- 递归划分(标准解) :后序的 最后一个 元素是根;在中序中找到该元素的位置,它左侧是左子树、右侧是右子树。
- 区间边界的推导 :设中序中根的下标为 k,左子树大小 left_size = k − il 。则后序中左子树区间为 [pl, pl + left_size − 1] ,右子树为 [pl + left_size, pr − 1] 。
- 关键:哈希表定位根 :预处理 值 → 中序下标 ,避免每次 O(n) 查找。
- 与 105 的对比 :105 的根在前序的 开头 ,本题的根在后序的 末尾 ,区间划分方式不同。
常见陷阱
- 把根取成 postorder[0] :后序的根在 末尾 。
- 右子树的区间写成 [pl + left_size + 1, pr] :应排除根节点(末尾),即 [pl + left_size, pr − 1] 。
- 用 inorder.index() 查找根 :O(n) 每次,整体 O(n²)。必须预建哈希表。
- 递归终止条件写成 il >= ir :会漏掉叶子节点。应为 il > ir 。
参考实现
class Solution:
def buildTree(self, inorder: list[int], postorder: list[int]) -> 'TreeNode':
idx = {v: i for i, v in enumerate(inorder)} # 值 -> 中序下标
def build(il: int, ir: int, pl: int, pr: int):
if il > ir:
return None
root = TreeNode(postorder[pr]) # 后序末尾是根
k = idx[root.val]
left_size = k - il
root.left = build(il, k - 1, pl, pl + left_size - 1)
root.right = build(k + 1, ir, pl + left_size, pr - 1)
return root
return build(0, len(inorder) - 1, 0, len(postorder) - 1)