当前位置:网站首页>剑指 Offer 07. 重建二叉树
剑指 Offer 07. 重建二叉树
2022-06-22 20:52:00 【前端粉刷匠】
输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
例子:
前序遍历 preorder = [3,9,20,15,7]
中序遍历 inorder = [9,3,15,20,7]
输出:
3
/ \
9 20
/ \
15 7
首先我们需要了解一下二叉树的前中后序遍历的规则
1、前序遍历:中左右
2、中序遍历:左中右
3、后序遍历:左右中
上面的规则很简单我们只需要根据中的位置判断就行,左始终是在右的左边,反正右始终在左的右边
解法1:递归
思路分析:首先我们算法首先需要判空,同时递归也需要首先找到结束递归的点,在这个题目中结束的点就是两个形参为空,根据我们上面了解到的规则,我们可以总结出,前序遍历的第一个值就是二叉树的根结点,后续遍历的中间节点是数的根节点,所以我门使用前序遍历的第一个参数,去和中序遍历的各个节点对比,找到相同的那个,找到的这个值的左边是左子树,右边的都是又子树,以此类推我们就可以总结出递归规则
let preorder = [3, 9, 20, 15, 7]; // 前
let inorder = [9, 3, 15, 20, 7]; // 中
function TreeNode(val) {
this.val = val;
this.left = this.right = null;
}
function buildTree(preorder, inorder) {
// 判空
if (!preorder.length || !inorder.length) {
return null;
}
const rootVal = preorder[0];
const node = new TreeNode(rootVal);
let i = 0;
for ( i = 0; i < inorder.length; i++) {
if (inorder[i] === rootVal) {
break;
}
}
// 这里寻找一个数在数组中的位置,方法很多,就不一一列举了
node.left = buildTree(preorder.slice(1, i + 1), inorder.slice(0, i))
node.right = buildTree(preorder.slice(1 + i), inorder.slice(i + 1))
return node
}
边栏推荐
- From 11 hours to 25 seconds -- is there room for optimization?
- Mysql database design
- 2021-08-22
- Use full function simulation design method
- Core and semiconductor "RF eda/ filter design platform" shines ims2022
- Considerations for using redisson to operate distributed queues
- Spark SQL Start(2.4.3)
- Codeup longest palindrome substring
- How much do you know about the cause of amplifier distortion?
- 别再用 System.currentTimeMillis() 统计耗时了,太 Low,StopWatch 好用到爆!
猜你喜欢

ArcGIS application (20) the ArcGIS grid image symbol system prompts "this dataset does not have valid histogram required for classificati..."

安装typescript环境并开启VSCode自动监视编译ts文件为js文件

Explain the startup process of opengauss multithreading architecture in detail

leetcode. 11 --- container with the most water

c语言---17 函数简介

Reinforcement learning weekly (issue 50): saferl kit, gmi-drl, rp-sdrl & offline meta reinforcement learning

Seriously, the hang up of the kotlin collaboration process is not so mysterious (principle)

保证数据库和缓存的一致性

Three cache methods and principles

2021-03-06
随机推荐
Introduction to database access tools
The method of making videos of knowledge payment system support m3u8 format playback
Practice brings true knowledge: the strongest seckill system architecture in the whole network is decrypted. Not all seckills are seckills!!
How to change the dial on the apple Watch
The mental process and understanding of visual project code design
LinkedList source code analysis
2021-04-14
Redis big key problem
2021-04-14
How to quickly build an enterprise knowledge base at low cost?
js----SVG转PNG
2021-05-02
R language data preprocessing, converting type variables into factor variables, converting data sets into H2O format, and dividing data sets (training set, test set, verification set)
Zynq ultrascale + rfsoc zcu111 RF clock tree learning 1
2021-08-21
Several ways of redis persistence -- deeply parsing RDB
Reasons for the failure of digital transformation and the way to success
MySQL constraints
获取鼠标移动的方向
The xinjietu x70s has been listed for 87900 times and has leapfrogged the class in space safety. It is worthy of being a 7-seat SUV of the National University of China