​LeetCode解法汇总106. 从中序与后序遍历序列构造二叉树

news/2024/7/5 3:48:25

 目录链接:

力扣编程题-解法汇总_分享+记录-CSDN博客

GitHub同步刷题项目:

https://github.com/September26/java-algorithms

原题链接:

力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台


描述:

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

示例 1:

输入:inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
输出:[3,9,20,null,null,15,7]

示例 2:

输入:inorder = [-1], postorder = [-1]
输出:[-1]

提示:

  • 1 <= inorder.length <= 3000
  • postorder.length == inorder.length
  • -3000 <= inorder[i], postorder[i] <= 3000
  • inorder 和 postorder 都由 不同 的值组成
  • postorder 中每一个值都在 inorder 中
  • inorder 保证是树的中序遍历
  • postorder 保证是树的后序遍历

解题思路:

1.和105题差不多,只不过后序遍历的最后一个数字,一定是根节点。中序遍历中找到这个根节点,就可以分成左右两份,分别对应左子树和右子树。

2.所以我们使用和105题一样的策略,唯一要改的就是传入的区间范围。

代码:

/**
 * 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)
    {
        vector<int> indexMap(6000);
        for (int i = 0; i < inorder.size(); i++)
        {
            indexMap[inorder[i] + 3000] = i;
        }
        TreeNode *node = buildNode(indexMap, postorder, inorder, 0, postorder.size() - 1, 0, inorder.size() - 1);
        return node;
    }

    TreeNode *buildNode(vector<int> &indexMap, vector<int> &postorder, vector<int> &inorder, int postStart, int postEnd, int inStart, int inEnd)
    {
        int expectValue = postorder[postEnd];
        int index = indexMap[expectValue+3000];
        int leftLength = index - inStart;
        int rightLength = inEnd - index;
        TreeNode *root = new TreeNode(expectValue);
        if (leftLength >= 1)
        {
            root->left = buildNode(indexMap, postorder, inorder, postStart, postStart + leftLength - 1, inStart, index - 1);
        }
        if (rightLength >= 1)
        {
            root->right = buildNode(indexMap, postorder, inorder, postEnd - rightLength, postEnd - 1, index + 1, inEnd);
        }
        return root;
    }
};


http://lihuaxi.xjx100.cn/news/2041678.html

相关文章

学习鸿蒙一定要搞清楚的几个概念

目录 1、UI框架 2、应用模型 2.1、应用模型介绍 2.2、两种应用模型 2.3、应用模型和UI框架的关系 3、Ability 3.1、Ability介绍 3.2、FA模型的ability 3.3、Stage模型的Ability 1、UI框架 HarmonyOS提供了一套UI(User Interface,用户界面)开发框架&#xff0c;即方舟…

BUUCTF crypto做题记录(8)新手向

一、密码学心声 得到信息如下图 背景故事没什么信息&#xff0c;主要看曲谱。大概率不会让我们涉及与音乐有关的内容&#xff0c;题目中也提示说答案是一串字符串&#xff0c;所以我们可以猜测是将曲谱上的数字转化成字符。曲谱中文字提示是用ASCII码进行转换。没有数字8可能是…

08.STL队列

1.基本操作 #include<iostream> #include <stack> #include<queue> using namespace std; queue<int> q; stack<int> stk; void text01(){stk.push(1);stk.push(2);stk.push(3);stk.push(4);q.push(5);q.push(6);q.push(7);q.push(8);cout<…

基于Spring Boot的安康旅游网站的设计与实现,计算机毕业设计(带源码+论文)

源码获取地址&#xff1a; 码呢-一个专注于技术分享的博客平台一个专注于技术分享的博客平台,大家以共同学习,乐于分享,拥抱开源的价值观进行学习交流http://www.xmbiao.cn/resource-details/1760645517548793858

【leetcode刷题之路】面试经典150题(4)——栈+链表

文章目录 7 栈7.1 【哈希表】有效的括号7.2【栈】简化路径7.3 【栈】最小栈7.4 【栈】逆波兰表达式求值7.5 【栈】基本计算器 8 链表8.1 【双指针】环形链表8.2 【双指针】两数相加8.3 【双指针】合并两个有序链表8.4 【哈希表】随机链表的复制8.5 【链表】反转链表 II8.6 【链…

穿越Redis单线程迷雾:从面试场景到技术内核的解读

目录 ​编辑 前言 Redis中的多线程 I/O多线程 Redis中的多进程 结论 延伸阅读 前言 很多人都遇到过这么一道面试题&#xff1a;Redis是单线程还是多线程&#xff1f;这个问题既简单又复杂。说他简单是因为大多数人都知道Redis是单线程&#xff0c;说复杂是因为这个答案…

五、全局scss变量定义及使用

定义 variable.scss 存放全局变量 // base color $blue:#324157; $light-blue:#3A71A8; $red:#C03639; $pink: #E65D6E; $green: #30B08F; $tiffany: #4AB7BD; $yellow:#FEC171; $panGreen: #30B08F;// 默认菜单主题风格 $base-menu-color:#bfcbd9; $base-menu-color-active:#f…

Linux:Jenkins用户权限和管理

1.下载插件 由于Jenkins的默认权限管理并不是很精细所以我们安装一个插件进行权限的一个管理 插件名称为&#xff1a;Role-based Authorization Strategy 安装完插件我们再去配置一下 进入全局安全配置 选择这个Role-Based Strategy策略然后保存 2.创建角色 我们这里主要使…