【力扣每日一题】力扣106从中序和后序遍历序列构造二叉树

题目来源

力扣106从中序和后序遍历序列构造二叉树

题目概述

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

思路分析

后序遍历序列的最末尾数据为树的根节点。 在中序遍历序列中找到树的根节点就可以找到这棵树的左子树范围和右子树范围。 分析方法与从前序与中序遍历序列构造二叉树类似。

代码实现

java实现

java 复制代码
public class Solution {

    Map<Integer, Integer> inorderIndexMap = new HashMap<>();
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        // 中序遍历序列数据与下标映射,便于后续查找
        for (int i = 0; i < inorder.length; i++) {
            inorderIndexMap.put(inorder[i],i);
        }
        return create(inorder, postorder ,0, inorder.length - 1, 0, postorder.length - 1);
    }

    private TreeNode create(int[] inorder, int[] postorder, int iStart, int iEnd, int pStart, int pEnd) {
        if (pEnd < pStart) {
            return null;
        }
        // 构建当前子树根节点
        int current = postorder[pEnd];
        TreeNode root = new TreeNode(current);

        // 当前节点在中序遍历序列的位置
        int rootIndexInInorder = inorderIndexMap.get(current);
        // 右子树长度
        int rightSubTreeSize = iEnd - rootIndexInInorder;

        // 构建左右子树
        root.right = create(inorder,postorder, rootIndexInInorder + 1, iEnd ,pEnd - rightSubTreeSize, pEnd - 1);
        root.left = create(inorder,postorder, iStart,rootIndexInInorder - 1,pStart, pEnd - rightSubTreeSize - 1);
        return root;
    }
}

c++实现

cpp 复制代码
class Solution {
public:
    unordered_map<int, int> inorder_data_and_index;
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        // 中序遍历序列数据与下标映射,便于后续查找
        for (int i = 0; i < inorder.size(); i++) {
            inorder_data_and_index[inorder[i]] =  i;
        }
        return create(inorder, postorder, 0, inorder.size() - 1, 0, postorder.size() - 1);
    }

    TreeNode* create(vector<int>& inorder, vector<int>& postorder, int iStart, int iEnd, int pStart, int pEnd) {
        if (pEnd < pStart) {
            return nullptr;
        }
        // 构建当前子树根节点
        int current = postorder[pEnd];
        TreeNode* root = new TreeNode(current);

        // 当前节点在中序遍历序列的位置
        int rootIndexInInorder = inorder_data_and_index[current];
        // 右子树长度
        int rightSubTreeSize = iEnd - rootIndexInInorder;

        // 构建左右子树
        root->right = create(inorder, postorder, rootIndexInInorder + 1, iEnd, pEnd - rightSubTreeSize, pEnd - 1);
        root->left = create(inorder, postorder, iStart, rootIndexInInorder - 1, pStart, pEnd - rightSubTreeSize - 1);
        return root;
    }
}
相关推荐
Zane19947 小时前
ThreadLocal 与 BlockingQueue:线程本地变量的内存泄漏原理,以及阻塞队列怎么选?
java·后端
用户3126874877207 小时前
Spring Bean 生命周期到底经历了什么?从实例化到销毁的全链路拆解
java·spring boot
choumin7 小时前
C程序能调用extern “C“声明的带默认参数的C++函数吗?
c语言·c++·extern c·默认参数
Flittly7 小时前
【我的手搓轮子日记】(2)handmade-ioc 手搓 IOC 容器
java·spring boot·spring
阿昌喜欢吃黄桃7 小时前
Dubbo服务重启时注册中心下线不及时问题排查与修复记录
java·rpc·dubbo·问题排查
confiself7 小时前
树形思考研究进展
java·大数据·微服务
AmyLin_20017 小时前
PDF 色彩保真工程实践【3】上手实践:工程结构、依赖集成与一键构建运行
c++·visualstudio·pdf·zlib·vcpkg·libtiff·cmyk
还是鼠鼠7 小时前
【Spring AI智能体实战 01】Java开发者接入大模型:国内平台、Token与API Key入门
java·大模型·spring ai·deepseek·阿里云百炼
并不喜欢吃鱼7 小时前
从零开始 C++------ 十六.深度拆解 C++ 智能指针:unique_ptr/shared_ptr/weak_ptr 底层模拟、内存泄漏根治
开发语言·c++
zerozrc07 小时前
Java Lambda 底层实现
java