面试经典150题[073]:从中序与后序遍历序列构造二叉树(LeetCode 106)

从中序与后序遍历序列构造二叉树(LeetCode 106)

题目链接:从中序与后序遍历序列构造二叉树(LeetCode 106)

难度:中等

1. 题目描述

给你两个整数数组 inorder 和 postorder ,其中 inorder 是二叉树的中序遍历,postorder 是同一棵树的后序遍历,请你构造并返回这颗二叉树。

要求:

  • 树中没有重复元素
  • 数组长度 1 <= n <= 3000
  • -3000 <= inorderi, postorderi <= 3000
  • inorder 和 postorder 都由唯一值组成,且 postorder 中的每个值都在 inorder 中出现
  • inorder 保证是树的中序遍历
  • postorder 保证是树的后序遍历

示例:

复制代码
输入: inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
输出: [3,9,20,null,null,15,7]
解释: 二叉树结构为根节点3,左子节点9,右子节点20(左15,右7)。

输入: inorder = [2,1], postorder = [2,1]
输出: [1,2]
解释: 根节点1,左子节点2。

2. 问题分析

2.1 规律

  • 中序遍历:左子树 -> 根 -> 右子树。
  • 后序遍历:左子树 -> 右子树 -> 根。
  • 后序遍历的最后一个元素总是当前子树的根节点。
  • 在中序遍历中找到根节点的位置,其左侧是左子树的中序,右侧是右子树的中序。
  • 由于值唯一,可用哈希表存储中序值到索引的映射,避免每次线性搜索。
  • 核心问题:如何递归划分左右子树,并高效构建树结构?

2.2 递归思路

我们使用递归算法:

  • 预构建哈希表 inorder_map,存储每个值在中序数组中的索引。
  • 使用后序数组从末尾开始弹出根节点(因为后序最后是根)。
  • 递归函数 helper(in_start, in_end):
    • 如果 in_start > in_end,返回 None(空树)。
    • 取当前根值 root_val = postorder[post_idx],post_idx -= 1。
    • 创建根节点。
    • 找到根在中序的索引 idx = inorder_map[root_val]。
    • 先递归构建右子树 root.right = helper(idx + 1, in_end)(因为后序中右子树在左子树之后,从后往前先遇到右子树)。
    • 再递归构建左子树 root.left = helper(in_start, idx - 1)。
  • 初始调用:helper(0, n-1),post_idx 初始化为 n-1。

3. 代码实现

Python

python 复制代码
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
        n = len(inorder)
        if n == 0:
            return None
        
        inorder_map = {val: idx for idx, val in enumerate(inorder)}
        post_idx = n - 1
        
        def helper(in_start: int, in_end: int) -> Optional[TreeNode]:
            nonlocal post_idx
            if in_start > in_end:
                return None
            
            root_val = postorder[post_idx]
            post_idx -= 1
            root = TreeNode(root_val)
            idx = inorder_map[root_val]
            
            root.right = helper(idx + 1, in_end)
            root.left = helper(in_start, idx - 1)
            
            return root
        
        return helper(0, n - 1)

C++

cpp 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        int n = inorder.size();
        if (n == 0) return nullptr;
        
        unordered_map<int, int> inorder_map;
        for (int i = 0; i < n; ++i) {
            inorder_map[inorder[i]] = i;
        }
        
        int post_idx = n - 1;
        
        function<TreeNode*(int, int)> helper = [&](int in_start, int in_end) -> TreeNode* {
            if (in_start > in_end) return nullptr;
            
            int root_val = postorder[post_idx];
            --post_idx;
            TreeNode* root = new TreeNode(root_val);
            int idx = inorder_map[root_val];
            
            root->right = helper(idx + 1, in_end);
            root->left = helper(in_start, idx - 1);
            
            return root;
        };
        
        return helper(0, n - 1);
    }
};

4. 复杂度分析

  • 时间复杂度:O(n),构建哈希表 O(n),递归遍历每个节点一次 O(n)。
  • 空间复杂度:O(n),哈希表 O(n),递归栈最坏 O(n)(链状树)。

5. 总结

  • 树重建问题 + 中序+后序 → 递归划分子树是首选。
  • 核心维护后序索引和中序映射,很通用。
    • 类似从中序+前序构建树(LeetCode 105),但调整构建顺序(前序先左后右,后序先右后左)。
    • 可扩展到其他遍历组合或序列化问题。

复习

面试经典150题013:除自身以外数组的乘积(LeetCode 238)

面试经典150题043:字母异位词分组(LeetCode 49)

面试经典150题058:两数相加(LeetCode 2)

相关推荐
卷福同学3 小时前
第一次当面试官有感
后端·面试
怕浪猫3 小时前
顶级模型一句话,AI 写出了能玩的 QQ飞车
前端·面试·github
呃呃呃呃ex3 小时前
9. TypeScript:从类型系统到工程实践
前端·面试
Blanche15003 小时前
Webpack 构建流程解析
javascript·面试
我叫黑大帅3 小时前
为了知道计算机为什么选择了二进制,我做了这些……
面试·程序员·计算机组成原理
怕浪猫3 小时前
DeepSeek Harness 系列图解
面试·前端框架·node.js
double_face3 小时前
AgentScope 2.0 源码解析系列(Java 版)|第 3 篇:State 模块,Agent 靠什么记住这轮对话
后端·面试
leavesleo3 小时前
KV Cache 压到 4bit 之后:SGLang 在 Blackwell 上的 1.78 倍上下文和 +78% 解码
面试
用户6802659051193 小时前
企业电脑统一管理怎么做?2026企业终端统一管理方法与工具推荐
javascript·后端·面试
RootSeeker3 小时前
Docker 里跑排障运行时,我先核对这几个点
后端·面试