【力扣每日一题】力扣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;
    }
}
相关推荐
吴声子夜歌24 分钟前
Guava——散列
java·guava
用户31268748772025 分钟前
AQS 到底怎么排队的?CLH 队列、state 与条件变量全链路拆解
java
Json____32 分钟前
springboot开发高校选课管理系统:从零构建前后端分离的全栈实践
java·spring boot·后端·毕业设计·毕设·wwwoop.com
xcl092542 分钟前
民宿预约系统开发实战:从需求分析到上线部署全流程解析
java·spring boot
步行cgn1 小时前
为何以继承方式引入SpringBoot
java·spring boot·后端
CoderYanger1 小时前
A.每日一题:1140. 石子游戏 II
java·程序人生·算法·leetcode·游戏·职场和发展·深度优先
禾高网络1 小时前
养老系统开发:背景、架构与养老行业展望
java·大数据·人工智能·小程序·架构
小雨笙笙1 小时前
机器学习:评估模型与选择
人工智能·算法·机器学习
高亦真1 小时前
今天是学习嵌入式的第35天
linux·学习·算法
mengzhi啊1 小时前
QEventLoop loop配合loop.exec();替代while(1)。电脑cpu占用为0,使用while(1)cpu占用率100%
c++·qt