LeetCode:Construct Binary Tree from Preorder and Inorder Traversal

Xilnx大学计划/XUP ZYNQ ZU开发板 TUL PYNQ™-ZU板,基于Xilinx Zynq UltraScale+ MPSoC,是为Xilinx大学项目设计的,以支持Xilinx PYNQ (Python Productivi-ty for Zynq)开源框架和嵌入式系统开发。 阅读详情

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.

题意:根据二叉树的先序和中序遍历的结果来构建此二叉树(二叉树中不包含值相同的节点)

解题思路:在数据结构课程中我们知道可以由二叉树的先序和中序或者中序和后序的遍历结果来构建此二叉树,具体步骤如何呢?我们举个具体的例子来分析。

如图,我们可以分三步:1、将先序遍历序列的第一个值作为根节点(如将6作为根节点);2、从中序遍历中寻找第一步的节点在此序列的下标(如6在此下标为2);3、根据下标更新遍历序列,递归构建此根节点的左右子树,进入步骤1.


//p_s、p_e表示在先序遍历数组中的起始位置和结束位置,i_s、i_e表示中序遍历数组的起始位置和结束位置
TreeNode* buildNodes(vector<int>& preorder, int p_s, int p_e, vector<int>&inorder, int i_s, int i_e)
{
	if (p_s > p_e || i_s > i_e)
		return NULL;

	//先序遍历的第一个节点是作为根节点
	TreeNode* node = new TreeNode(preorder[p_s]);
	
	//从中序遍历中找到该节点的位置
	int nodeIndex = i_s;
	while (nodeIndex<=i_e && preorder[p_s] != inorder[nodeIndex])
		nodeIndex++;
	
	//在中序遍历中,计算该节点离此次遍历起始位置的长度
	int leftLen = nodeIndex - i_s;
	//构建左子树,更新遍历范围
	node->left = buildNodes(preorder, p_s + 1, p_s + leftLen, inorder, i_s, nodeIndex - 1);

	//构建右子树,更新遍历范围
	node->right = buildNodes(preorder, p_s + leftLen + 1, p_e, inorder, nodeIndex + 1, i_e);

	return node;
}

TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) 
{
	if (preorder.size() != inorder.size() || preorder.size() == 0)
		return NULL;
	
	return buildNodes(preorder, 0, preorder.size()-1, inorder, 0, inorder.size()-1);
}

通信原理 | python调制识别数据及代码 数据集包含了从-20dB 到+18dB 总共 20 个信噪比(步长为 2)下的 11 种调制信号, 包括 AM-DSB、 AM-SSB 和 WBFM 三种模拟调制信号,以及 BPSK、 QPSK、 8PSK、 CPFSK、 GFSK、 PAM4、 QAM16 和 QAM64 八种数字调制信号。其中信号的中心频率为 200KHz,采样频率为 1Msamp/s,且每个信噪比下每种调制信号包含 1000 个信号。其中每个信号包含 IQ 两路数据,且每一路数据都包含有 128 个采样点。 阅读详情

相关推荐

【STM32】STM32F407实现简单的Modbus协议

1.ModBus协议简介 MODBUS协议是一种串行通信协议,由Modicon公司(施耐德公司前身)发表,由于其公开发表且无版权要求,易于部署和维护,在工业界广泛应用。MODBUS采用主从通信(Master/Slave),MODBUS有三种报文格式:ASCII、RTU、TCP,本文主要讨论RTU。 如下图所示,串行通信上的MODBUS协议主要由地址,功能码,数据,CRC校验四部分数据帧构成。主机和从机的串行通信设备要求一致,参数要求一致,即假设使用串口,主从机波特率,奇偶校验...

BadBoyHolly的博客 8183

LeetCode 105:Construct Binary Tree from Preorder and Inorder Traversal

LeetCode 105: Given preorder and inorder traversal of a tree, construct the binary tree. 给定一个二叉树的前序和中序遍历,重建这棵二叉树。

哈哈的个人专栏 1688

SLAM轨迹评估神器evo实战:从GPS到TUM格式的完整转换指南(附Python代码)

本文详细介绍了如何使用evo工具将GPS数据转换为TUM格式,以便进行SLAM轨迹评估。通过Python代码实现GPS到笛卡尔坐标的精确转换,并提供了完整的处理流程和实战案例,帮助开发者轻松完成从原始GPS数据到精确轨迹评估的全过程。特别适用于ORB-SLAM等视觉SLAM算法的性能验证。

weixin_29074295的博客 292

[LeetCode] 021: Construct Binary Tree from Preorder and Inorder Traversal

[Problem]Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. [Solution]/** * Definition for binary tree * struct

1.02^365的成长裂变 3672

Cadence Sigrity

目录 一、PowerSI 1、PowerSI仿真前设置 2、PowerSI提取S参数 (1)检查叠层 (2)选择需要提取的走线和GND (3)设置端口 (4)设置提取频率和提取方式 (5)运行仿真 一、PowerSI PowerSI是一个交流分析工具,适用于频域仿真。 S参数提取 电源阻抗提取 全波参数提取 阻抗、耦合检查 谐振分析 1、PowerSI仿真前设置 打开PCB文件: 打开后如图: 设置为单色后: 仿真前的设置:

fang_yang_wa的博客 3万+

【数据集6】全球人工不透水面积GAIA(清华数据)

全球人工不透水面GAIA简介及其应用

WW、forever的博客 3800

Leetcode: 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. 根据前序遍历和中序遍历结果构造二叉树。思路分析: 分析二叉树前序遍历和中序遍历的结果我们发现: 二叉树中序遍

给永远比拿愉快 1349

ArcMap|使用模型构建器将kml文件批量转图层

参考:https://www.jianshu.com/p/6de18f846024 第一步:打开模型构建器,拖入kml转图层工具 第二步:插入文件迭代器 第三步:双击“迭代文件”,选择kml所在文件夹 第四步:将“File.kml”与“KML转图层”连接,点击“输入KML文件” 第五步:双击“KML转图层”,更改输出位置,输出数据名称改为%名称% 第六步:运行模型 得到结果: ...

个人学习笔记 3601

LeetCode[Tree]: 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. 这类问题非常适合用递归做,递归思路如下: 前序遍历的第一个节点必然是根节点,中序遍历中根节点之前的节

chfe910的专栏 873

Python爬虫实战:基于最新技术栈的政府招标采购数据抓取与分析

对于企业、研究人员和数据分析师来说,能够高效地获取这些数据意味着可以获得宝贵的商业洞察和市场先机。随着反爬技术的不断升级和网站结构的日益复杂,传统的爬虫方法已经难以满足需求。我们将采用Selenium、Playwright等现代浏览器自动化工具,结合Scrapy框架和异步请求库,构建一个能够应对各种反爬措施的强大爬虫系统。Playwright是微软开发的浏览器自动化工具,支持Chromium、WebKit和Firefox,比Selenium更快速、更稳定。

2201_76125261的博客 1311

高通SDX35:SGMII接口PHY RTL8211FSI-CG驱动适配

高通SDX35 RTL8211F SGMII

不秃头的程序猿Gabriel的博客 785

[C++]LeetCode: 37 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. Anwser 1: 递归法 思路解析: 这道题是树中比较有难度的题目,需要根据先序遍历和中序

cindy_niu的专栏 810

飞牛fnNAS搭建Web网页版OFFICE(WPS)软件

本文介绍了如何在飞牛NAS上通过Docker容器部署WPS Office办公软件。主要内容包括:启用Docker服务、准备项目文件夹结构、使用Compose方式构建容器、解决端口冲突问题、配置中文输入法和字体库,以及通过ZeroTier实现远程访问。文章详细说明了每个步骤的操作方法,包括容器启动、输入法设置、字体添加和文件存储位置配置。最后还介绍了使用ZeroTier内网穿透技术实现远程访问NAS上WPS服务的方法,让用户可以在不同设备上随时编辑NAS中的文件。整个部署过程完整清晰,为NAS用户提供了便捷的

dgnankai的专栏 1699

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

【题目】 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 【题意】 给定先序和中序遍历,构造二叉树。 题目保证所给的序列中不存在重复值 【思路】 递归求解:以根节点为例,先序遍历的第一个值就是根节点的值,我们在中序遍历中找到根节点值的位置,在该位置之前的

Harry Huang 1063

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

leetcode题解:第105题Construct Binary Tree from Preorder and Inorder Traversal

https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 文章目录解法一、递归代码复杂度分析解法二、迭代 解法一、递归 前序遍历的序列组成形式为:根, {左子树前序结果}, {右子树前序结果} 中序遍历的序列组成形式为:{左子树中序结果}, 根, {右子树中序结果} 把上面这两行写出来,递归解法就很容易想到了。 很明显,我们需要知道左子树的节点个数,才好获取到左子树的前序结果。由于树

chenf1999的博客 345

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

leetcode笔记:Construct Binary Tree from Preorder and Inorder Traversal

这道题考察了先序和中序遍历,先序是先访问根节点,然后访问左子树,最后访问右子树;中序遍历是先遍历左子树,然后访问根节点,最后访问右子树。

liyuefeilong的专栏 1905

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

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...

卷卷萌的博客 218

【LeetCode】解题105:Construct Binary Tree from Preorder and Inorder Traversal

LeetCode解题 105:Construct Binary Tree from Preorder and Inorder Traversal Problem 105: Construct Binary Tree from Preorder and Inorder Traversal [Medium]解题思路Solution (Java) Problem 105: Construct Binar...

Fayedily的博客 208

leetcode:二叉树之Construct Binary Tree from Preorder and Inorder Traversal

leetcode:二叉树之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 exi

what_lei的博客 457

LEETCODE: 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. 只会递归的方法。 /** * Definition for binary tree * struct Tre

博客已退休 406

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,...

qq_34314736的博客 131

LeetCode 105: Construct Binary Tree From Preorder and Inorder Traversal题解(python)

LeetCode 105: Construct Binary Tree From Preorder and Inorder Traversal 分类:Tree & Recursion 难度:M 描述:给了一个树的先序遍历和中序遍历的结果,让把这歌二叉树给复现出来。 preorder = [3,9,20,15,7] inorder = [9,3,15,20,7] Tree: 3 / \ 9 20 / \ 15 7 链接: Construct Binary T

Bourns_A的博客 373

leetcode_105:Construct Binary Tree from Preorder and Inorder Traversal

这道题目是根据前序和中序遍历,构造一棵二叉树。数据结构里面的,但是做的时候还是没有头绪,因为有一个我的软肋(我全身没有什么硬肋骨):递归。但是还是硬着头皮,分析了一下,最后总算做出来了,但是结果却并不满意:慢的要死内存还很大…。想着先整理一下思路,然后看看别人的代码有什么特别之处。 先说一下思路,抛开编程,根据两个序列构造一个二叉树在数据结构中会出现这种题目,基本上就是在前序遍历中找根,在中序遍历...

reachwang的博客 116

《leetCode》: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.思路这个题目再《剑指Offer》中做过,具体见http://blog.youkuaiyun.com/u010412719/article/

wojiushimogui的博客 382
上一篇: Binary Tree Maximum Path Sum
下一篇: LeetCode : Partition List
FightForProgrammer
博客等级 码龄13年 239粉丝 75原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值