【力扣每日一题】力扣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;
    }
}
相关推荐
半桔1 小时前
【IO多路转接】高并发服务器实战:Reactor 框架与 Epoll 机制的封装与设计逻辑
linux·运维·服务器·c++·io
JH30732 小时前
SpringBoot 优雅处理金额格式化:拦截器+自定义注解方案
java·spring boot·spring
HABuo2 小时前
【linux文件系统】磁盘结构&文件系统详谈
linux·运维·服务器·c语言·c++·ubuntu·centos
我在人间贩卖青春2 小时前
C++之多重继承
c++·多重继承
颜酱2 小时前
图结构完全解析:从基础概念到遍历实现
javascript·后端·算法
m0_736919103 小时前
C++代码风格检查工具
开发语言·c++·算法
yugi9878383 小时前
基于MATLAB强化学习的单智能体与多智能体路径规划算法
算法·matlab
Coder_Boy_3 小时前
技术让开发更轻松的底层矛盾
java·大数据·数据库·人工智能·深度学习
DuHz3 小时前
超宽带脉冲无线电(Ultra Wideband Impulse Radio, UWB)简介
论文阅读·算法·汽车·信息与通信·信号处理
invicinble3 小时前
对tomcat的提供的功能与底层拓扑结构与实现机制的理解
java·tomcat