leetcode---105. Construct Binary Tree from Preorder and Inorder Traversal C++题解

Construct Binary Tree from Preorder and Inorder Traversal Construct Binary Tree from Preorder and Inorder Traversal 根据先序,中序建立二叉树 立即下载

105. Construct Binary Tree from Preorder and Inorder Traversal

这道题剑指offer已经做过了,见重建二叉树

直接复制思路:

C++。首先要知道一个结论,前序/后序+中序序列可以唯一确定一棵二叉树,所以自然而然可以用来建树。

看一下前序和中序有什么特点,前序1,2,4,7,3,5,6,8 ,中序4,7,2,1,5,3,8,6;

有如下特征:

  1. 前序中左起第一位1肯定是根结点,我们可以据此找到中序中根结点的位置rootin;
  2. 中序中根结点左边就是左子树结点,右边就是右子树结点,即[左子树结点,根结点,右子树结点],我们就可以得出左子树结点个数为int left = rootin - leftin;;
  3. 前序中结点分布应该是:[根结点,左子树结点,右子树结点];
  4. 根据前一步确定的左子树个数,可以确定前序中左子树结点和右子树结点的范围;
  5. 如果我们要前序遍历生成二叉树的话,下一层递归应该是:
    • 左子树:root->left = pre_order(前序左子树范围,中序左子树范围,前序序列,中序序列);;
    • 右子树:root->right = pre_order(前序右子树范围,中序右子树范围,前序序列,中序序列);。
  6. 每一层递归都要返回当前根结点root;

代码如下:

class Solution {
public:
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        return pre_order(0, inorder.size() - 1, 0, inorder.size() - 1, preorder, inorder);
    }
    
    TreeNode *pre_order(int leftpre, int rightpre, int leftin, int rightin, vector<int> &pre, vector<int> &in) {
        if (leftpre > rightpre || leftin > rightin) return NULL;
        TreeNode *root = new TreeNode(pre[leftpre]);
        int rootin = leftin;
        while (rootin <= rightin && in[rootin] != pre[leftpre]) rootin++;
        int left = rootin - leftin;
        root->left = pre_order(leftpre + 1, leftpre + left, leftin, rootin - 1, pre, in);
        root->right = pre_order(leftpre + left + 1, rightpre, rootin + 1, rightin, pre, in);
        return root;
    }
};
[V-10]ARMv8/v9-CPU Virtualization-CPU虚拟化架构简介(vCPU/vPE) 本文简要介绍了CPU虚拟化架构的基础知识。 阅读详情

相关推荐

跟着团子学SAP CO:基于成本的完工百分比结算:Cost-based POC(以项目为例)

本文以英国及北美会计准则下的基于成本的POC的销售型项目结算对PS模块中的完工百分比进行介绍

Lucas1211的博客 2万+

105. Construct Binary Tree from Preorder and Inorder Traversal

题目描述(中等难度) 根据二叉树的先序遍历和中序遍历还原二叉树。 解法一 递归 先序遍历的顺序是根节点,左子树,右子树。中序遍历的顺序是左子树,根节点,右子树。 所以我们只需要根据先序遍历得到根节点,然后在中序遍历中找到根节点的位置,它的左边就是左子树的节点,右边就是右子树的节点。 生成左子树和右子树就可以递归的进行了。 比如上图的例子,我们来分析一下。 preorder = [3,9,20,15,7] inorder = [9,3,15,20,7] 首先根据 preorder 找到根节点是 3 然后根

猫敷雪 412

CCS导入工程报错?手把手教你安装C2000编译器(附最新下载链接)

本文详细解析CCS导入工程报错的常见原因,并提供三种安装C2000编译器的解决方案(TI官网离线安装、CCS内置更新和App Center渠道)。通过版本匹配技巧和路径配置要点,帮助开发者快速解决编译器缺失问题,确保项目顺利导入和编译。

weixin_30374009的博客 669

LeetCode105: Construct Binary Tree from Preorder and Inorder Traversal(从前序与中序遍历序列构造二叉树)

Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. For example, given preorder = [3,9,20,15,7] inorder = [9,3,15,20,7] Return the following binary tree: 3 / \

凌凌岛的博客 384

直流电源EMI滤波器设计实战:从器件选型到PCB布局的完整指南

本文提供了一份直流电源EMI滤波器的完整实战设计指南。针对EMI噪声超标问题,文章深入剖析了差模与共模噪声的抑制原理,详细讲解了从电路拓扑、核心器件(如X/Y电容、共模电感)的选型计算,到决定性能成败的PCB布局艺术。重点强调了低阻抗路径、干净接地及输入输出隔离等关键布局原则,并分享了常见的调试坑点与排查方法,旨在帮助硬件工程师设计出真正有效的滤波器,确保电源系统通过EMC测试。

q9r0s的博客 654

博科的SAN交换机基本操作

2、添加用户testuser赋予user权限可以访问1-128VF默认VF为128账号描述"Test user"密码为password123。1、创建zone别名(100是指当前设备Domain ID,1和2是指对应的index号)2,删除新接入设备的ZONE或保持ZONE配置一致。3、将zone添加到当前生效的CFG中。2、将zone添加到当前生效的CFG中。3、将zone添加到当前生效的CFG中。3,将添加的级联端口持续关闭。4、将当前zone生效。4、将当前zone生效。1、查看当前所有用户。

chenjunokok的博客 1849

【LeetCode每日打卡】105. Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. For example, given preorder =[3,9,20,15,7] inorder = [9,3,15,20,7] Return the following binary tree: 3 ...

韩旭051的博客 703

基于STM32L4XX、HAL库的AD7124模数转换器ADC驱动程序设计

/ 检查ID是否正确 (AD7124-4/AD7124-8有不同的ID)// 转换为电压值 (假设使用内部2.5V基准)// 构造读命令: 1(R/W) + 1(0) + 6(寄存器地址)// 构造写命令: 0(R/W) + 1(0) + 6(寄存器地址).config = 0, // 使用配置0。内置可编程增益放大器(PGA):1-128倍。// 读取ADC数据。// 读取当前ADC控制寄存器。// 检查数据是否就绪。// 写回ADC控制寄存器。// 初始化AD7124。

zhusl6688的博客 535

【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51471280Subject 出处:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ Given preorder and inorder traversa

crazy_jack 8975

YOLO创新点改进点的消融实验设计规范

YOLO改进的消融实验是一种重要的方法,用于评估和验证模型中不同组件的影响。在进行YOLO改进的消融实验时,我们可以选择一些可能的改进点,比如Backbone网络、Anchor框设计和损失函数设计,然后通过实验来分析它们对模型性能的影响。首先,我们需要确定实验设计。对于每个改进点,我们将设计多个实验组,并针对每个实验组训练模型并进行性能评估。这里以三个改进点为例进行说明:改进点1:Backbone网络 (改进点2:Anchor框设计 (改进点3:损失函数设计 (

crasher123的博客 2919

SMPLify-x-master | 复现记录

由于毕业设计中将会用到smpl模型,而smplify方法的各环境依赖版本比较老,复现不太容易,因此便进行了smplifyx方法使用smplx的方法。由于在过程中遇到较多坑,因此进行记录和分享。

qq_44916003的博客 5114

经典:Construct Binary Tree from Preorder and Inorder Traversal:由先序中序构建二叉树

Given preorder and inorder traversal of a tree, construct the binary tree. 思路:手动模拟的时候,也是递归的重复。所以成序也是递归。为了减少重复时间,实现计算好中序中各个元素对应的下标位置。难点在于计算右子树时,需要计算偏移量。 这道题是必须会的一道题,非常非常的经典! /** * Definition for

小雄鹿的博客 274

【如何利用扩展卡尔曼滤波解决跟踪毫米波雷达目标】

笛卡尔坐标系(非极坐标系):使用两个垂直的轴(通常是x轴和y轴)来确定一个点的位置。你沿着这两个轴移动确定的距离来找到这个点。极坐标系:使用一个角度和一个距离来确定一个点的位置。首先,你转到一个特定的方向(角度),然后沿着那个方向移动确定的距离来找到这个点。希望这个例子能帮助你理解两种坐标系的基本概念!

小秋 AI SLAM 入门实战教程 906

【算法】【c++】【代码】105. Construct Binary Tree from Preorder and Inorder Traversal

经典题目,同理还有通过inorder和postorder构建binary tree Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/construct-binary-tree-fr

weixin_44420980的博客 216

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

ego-lite 完整指南:AI Agent 自动填表浏览器,复杂任务最多快 2.6 倍

你让 AI Agent 代你注册个账号,它大概率会卡在第一步:停在登录页要密码,还顺手动掉你正在看的标签页。ego-lite 就是为这个痛点设计的:一个你和 AI Agent 并行使用的浏览器,Agent 在隔离的任务空间里干活,直接继承你的登录态,自动填表类复杂任务最快完成速度高 2.6 倍。 [![ego-lite:AI Agent 浏览器自动化产品主视觉,人与 Agent 共用一个浏览器]

gitblog_00535的博客 783

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

算法设计与分析课作业【week3】leetode--105. Construct Binary Tree from Preorder and Inorder Traversal

题目 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. For example, given preorder = [3,9,20,15,7] inorder = ...

weixin_36332464的博客 423

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

LeetCode Hot 100 | 二叉树(下)· BST与高级操作(C++ 题解)

题号题目难度核心技巧时间复杂度98验证二叉搜索树🟡 中等中序遍历 + pre 记录上一节点,用 long long 避免溢出O(n)230BST 第 K 小的元素🟡 中等迭代中序遍历,找第 k 个节点即返回O(h+k)199二叉树的右视图🟡 中等BFS 层序遍历,记录每层最后一个非空节点O(n)114二叉树展开为链表🟡 中等后序递归,展开后把左子树接到右边,末尾接原右子树O(n²)105前序+中序构造二叉树🟡 中等。

jcm0903的博客 312

【LeetCode 面试经典150题】105. Construct Binary Tree from Preorder and Inorder Traversal 从前序和中序构造二叉树

Given two integer arrays and where is the preorder traversal of a binary tree and is the inorder traversal of the same tree, construct and return the binary tree.给定两个整数数组 和 ,其中 是二叉树的前序遍历,而 是同一棵树的中序遍历,根据这两个数组构造并返回这棵二叉树。输入: = [3,9,20,15,7], = [9,3,1

qq_43513394的博客 547

LintCode 73: Construct Binary Tree from Preorder and Inorder Traversal (二叉树经典题!)

Construct Binary Tree from Preorder and Inorder Traversal 中文English Given preorder and inorder traversal of a tree, construct the binary tree. Example Example 1: Input:[],[] Output:{} Explanation: T...

roufoo的博客 222

【算法】leetcode105. Construct Binary Tree from Preorder and Inorder Traversal从前序与中序遍历序列构造二叉树

这题给的是前序遍历和中序遍历。前序遍历的遍历顺序是根->左子树->右子树,中序遍历的遍历顺序是左子树->根->右子树。所以根据前序遍历找到根节点后,再从中序遍历中找到根的位置,从而知道左子树和右子树的大小,再根据大小,获得前序遍历的左子树和右子树,递归遍历这个过程。给定两个整数数组 preorder 和 inorder ,其中 preorder 是二叉树的先序遍历, inorder 是同一棵树的中序遍历,请构造二叉树并返回其根节点。是不变数组,用双指针。preorder 保证 为二叉树的前序遍历序列。

adsam123的博客 168

[LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal

105. Construct Binary Tree from Preorder and Inorder Traversal 通过树的前序和中序遍历重建二叉树

LLLZH 312

leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal(前序和中序遍历重建二叉树)

Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. For example, given preorder = [3,9,20,15,7] inorder = [9,3,15,...

蓝羽飞鸟的博客 326
上一篇: leetcode---103. Binary Tree Zigzag Level Order Traversal C++题解
下一篇: leetcode---106. Construct Binary Tree from Inorder and Postorder Traversal C++题解
宝宝可乖了
博客等级 码龄9年 56粉丝 40原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值