当前位置:网站首页>Leetcode刷题——构造二叉树(105. 从前序与中序遍历序列构造二叉树、106. 从中序与后序遍历序列构造二叉树)
Leetcode刷题——构造二叉树(105. 从前序与中序遍历序列构造二叉树、106. 从中序与后序遍历序列构造二叉树)
2022-08-04 11:06:00 【lonelyMangoo】
105. 从前序与中序遍历序列构造二叉树
题目:从前序与中序遍历序列构造二叉树
首先需要了解前序和中序遍历的一些概念
拿题目中的例子来说,从先序的第一个开始构造,选择3,在中序找到3,3左边的就是节点3左子树的内容,3右边的是节点3右子树的内容,所以在中序中3左边的个数就是左子树的长度,在中序中3右边的个数就是右子树的长度,这里就需要用map记下值和索引的对应关系方便查(能用map是因为无重复元素)。显然,由于先序遍历是从左边开始的,所以计算出左子树的长度,当前的下标+长度,这之前的是左子树上的,之后的是右子树上的。
下面代码:
Map<Integer, Integer> map;
public TreeNode buildTree(int[] preorder, int[] inorder) {
map = new HashMap<>(inorder.length);
//记录中序遍历值和下标的对应关系
for (int i = 0; i < inorder.length; i++) {
map.put(inorder[i], i);
}
TreeNode res = build( 0, inorder.length - 1,0, preorder, inorder);
return res;
}
//有返回值,因为拼接树要用到
//方法体中的方法只在当前时刻有用,不是全局的
private TreeNode build( int leftPre, int rightPre,int leftIn, int[] preorder, int[] inorder) {
//在确定左子树之后,得到左子树的长度,超过界限的值说明没有子树了。
if(leftPre>rightPre){
return null;
}
//根节点的值
int rootVal = preorder[leftPre];
TreeNode root = new TreeNode(rootVal);
//当前节点在中序序列中的位置
int indexInOrder = map.get(rootVal);
//左子树的长度
int leftLen = indexInOrder-leftIn;
//leftPre:当前值在先序中的位置
//rightPre:当前值在先序中最大的位置
//leftIn:inorder中的左边界(用于确定长度)
//探索左子树的时候,从当前节点左边,先序中第一个开始找
root.left= build(leftPre+1, leftPre+leftLen, leftIn , preorder, inorder);
//探索右子树
root.right= build(leftPre+leftLen+1, rightPre, indexInOrder+1 , preorder, inorder);
return root;
}
106. 从中序与后序遍历序列构造二叉树
题目:106. 从中序与后序遍历序列构造二叉树
后序遍历的做法是:从最后一个开始,每次都作为根节点,但是先构造的是右子树。
其余几乎一模一样,这不过这里要设置的是右边界。
public TreeNode buildTree(int[] inorder, int[] postorder) {
Map<Integer, Integer> map = new HashMap<>(inorder.length);
for (int i = 0; i < inorder.length; i++) {
map.put(inorder[i], i);
}
TreeNode res = build2( 0, inorder.length - 1,inorder.length - 1, postorder, inorder, map);
return res;
}
private static TreeNode build2( int leftPre, int rightPre,int rightIn, int[] postorder, int[] inorder, Map<Integer, Integer> map) {
//在确定左子树之后,得到左子树的长度
if(leftPre>rightPre){
return null;
}
//根节点的值
int rootVal = postorder[rightPre];
TreeNode root = new TreeNode(rootVal);
//当前节点在中序序列中的位置
int indexInOrder = map.get(rootVal);
//右子树的长度
int rightLen = rightIn-indexInOrder;
//rightIn为啥是-1,因为是从右往左的
root.right= build2(rightPre-rightLen, rightPre-1, rightIn , postorder, inorder, map);
root.left= build2(leftPre, rightPre-rightLen-1, indexInOrder-1 , postorder, inorder, map);
return root;
}
边栏推荐
- 【LeetCode】1403.非递增顺序的最小子序列
- 秒云成功入选《2022爱分析 · 银行数字化厂商全景报告》,智能运维能力获认可
- 单调栈一些题目练习
- 小程序容器加快一体化在线政务服务平台建设
- [Hongke case] Assembling furniture based on 3D camera
- Advanced transcriptome analysis and R data visualization hot registration (2022.10)
- Google Earth Engine APP——实现ui.Select() 的设定和条件判断
- RAID介绍及RAID5配置实例
- 技术干货 | 用零信任保护代码安全
- Win11 file types, how to change?Win11 modify the file suffix
猜你喜欢
Win11 file types, how to change?Win11 modify the file suffix
Redis查询缓存
Meishe Q&A Room | Meiying VS Meishe Cloud Editing
【机器学习】:如何对你的数据进行分类?
onlyoffice设置跟踪变化trackChanges默认为对自己启动
深度学习100例 —— 卷积神经网络(CNN)天气识别
图文手把手教程--ESP32 MQTT对接EMQX本地服务器(VSCODE+ESP-IDF)
使用.NET简单实现一个Redis的高性能克隆版(二)
华为开源:聚焦开源基础软件,共建健康繁荣生态
MySQL之my.cnf配置文件
随机推荐
Doing Homework HDU - 1074
Learn to use the basic interface of set and map
SkiaSharp 之 WPF 自绘 粒子花园(案例版)
图文手把手教程--ESP32 MQTT对接EMQX本地服务器(VSCODE+ESP-IDF)
Super Learning Method
【Idea series】idea configuration
职责链模式(responsibilitychain)
Mysql——》类型转换符binary
怎么禁止textarea拉伸
Google Earth Engine APP ——制作上传GIF动图并添加全球矢量位置
Camunda整体架构和相关概念
什么是终端特权管理
Mysql高级篇学习总结14:子查询优化、排序优化、GROUP BY优化、分页查询优化
强烈推荐一款优秀且通用的后台管理系统
Jenkins User Manual (1) - Software Installation
datax oracle to oracle incremental synchronization
Google Earth Engine APP——实现ui.Select() 的设定和条件判断
Zikko上市同时搭载HDMI2.1和2.5GbE新款雷电4扩展坞
知其然,知其所以然,JS 对象创建与继承
ECCV 2022 | 清华&腾讯AI Lab提出REALY: 重新思考3D人脸重建的评估方法