当前位置:网站首页>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;
}

边栏推荐
- AWS Lambda相关概念与实现思路
- zabbix deployment
- Win11怎么重装显卡驱动程序?Win11显卡驱动怎么卸载重装?
- Apache Calcite 框架原理入门和生产应用
- 萌宠来袭,如何让“吸猫撸狗”更有保障?
- 手搓一个“七夕限定”,用3D Engine 5分钟实现烟花绽放效果
- SkiaSharp 之 WPF 自绘 粒子花园(案例版)
- *iframe*
- Graphical Hands-on Tutorial--ESP32 One-Key Network Configuration (Smartconfig, Airkiss)
- Small program containers accelerate the construction of an integrated online government service platform
猜你喜欢

Camunda整体架构和相关概念

MySQL之my.cnf配置文件

C#/VB.NET:在 Word 中设置文本对齐方式

【虹科案例】基于3D相机组装家具

Use pytest hook function to realize automatic test result push enterprise WeChat

《迁移学习导论》第2版,升级内容抢先看!

helm安装

【机器学习】:如何对你的数据进行分类?

3-5年以上的功能测试如何进阶自动化?

JUC (1) threads and processes, concurrency and parallelism, thread state, locks, producers and consumers
随机推荐
8月活动|51CTO十七周年庆,发博文得茶具/笔记本/T恤等礼品!
粤黔协作,山海同心!578种贵州特色农产品走进粤港澳大湾区
apache dolphin scheduler 文件dolphinscheduler-daemon.sh详解
3-5年以上的功能测试如何进阶自动化?
STM32前言知识总结
cubemx stm32 afm3000 module gas flow sensor driver code
遍历Map的四种方法
数据化管理洞悉零售及电子商务运营——零售密码
ORB-SLAM3中的优化
ECCV 2022 | 清华&腾讯AI Lab提出REALY: 重新思考3D人脸重建的评估方法
命令模式(Command)
Rust 入门指南 (用 WASM 开发第一个 Web 页面)
关于架构的思考
【黄啊码】MySQL入门—2、使用数据定义语言(DDL)操作数据库
语音社交app源码——具备哪些开发优势?
MySQL不提供数组,只能做成表吗?
Jenkins User Manual (1) - Software Installation
tp5+微信小程序 分片上传
ROI LTV CPA ECPM体系讲解
【黄啊码】MySQL入门—1、SQL 的执行流程