当前位置:网站首页>Leetcode brush - structure binary tree (105. Once upon a time sequence and the sequence structure binary tree traversal sequence, 106. From the sequence with the sequence structure binary tree travers
Leetcode brush - structure binary tree (105. Once upon a time sequence and the sequence structure binary tree traversal sequence, 106. From the sequence with the sequence structure binary tree travers
2022-08-04 11:14:00 【lonelyMangoo】
105. 从前序与中序遍历序列构造二叉树
题目:从前序与中序遍历序列构造二叉树
First of all, you need to understand some concepts of preorder and inorder traversal
Take the example in the title,Construct from the first one in the preorder,选择3,Found in midorder3,3The left one is the node3The contents of the left subtree,3On the right is the node3The contents of the right subtree,So in the middle sequence3The number on the left is the length of the left subtree,在中序中3The number on the right is the length of the right subtree,这里就需要用mapWrite down the correspondence between the value and the index for easy checking(能用mapbecause there are no repeating elements).显然,Since preorder traversal starts from the left,So calculate the length of the left subtree,当前的下标+长度,The one before this is on the left subtree,The ones after that are on the right subtree.
下面代码:
Map<Integer, Integer> map;
public TreeNode buildTree(int[] preorder, int[] inorder) {
map = new HashMap<>(inorder.length);
//Record the correspondence between in-order traversal values and subscripts
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;
}
//有返回值,Because the splicing tree is used
//The methods in the method body are only available at the current moment,不是全局的
private TreeNode build( int leftPre, int rightPre,int leftIn, int[] preorder, int[] inorder) {
//After determining the left subtree,得到左子树的长度,Values that exceed the bounds indicate that there are no subtrees left.
if(leftPre>rightPre){
return null;
}
//根节点的值
int rootVal = preorder[leftPre];
TreeNode root = new TreeNode(rootVal);
//The position of the current node in the inorder sequence
int indexInOrder = map.get(rootVal);
//左子树的长度
int leftLen = indexInOrder-leftIn;
//leftPre:The position of the current value in the preorder
//rightPre:The position where the current value is the largest in the preorder
//leftIn:inorderthe left border in (Used to determine length)
//When exploring the left subtree,from the left of the current node,Start looking for the first one in the pre-order
root.left= build(leftPre+1, leftPre+leftLen, leftIn , preorder, inorder);
//explore the right subtree
root.right= build(leftPre+leftLen+1, rightPre, indexInOrder+1 , preorder, inorder);
return root;
}

106. 从中序与后序遍历序列构造二叉树
题目:106. 从中序与后序遍历序列构造二叉树
Post-order traversal is done:从最后一个开始,Each time as the root node,But the right subtree is constructed first.
其余几乎一模一样,But this is the right border to be set here.
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) {
//After determining the left subtree,得到左子树的长度
if(leftPre>rightPre){
return null;
}
//根节点的值
int rootVal = postorder[rightPre];
TreeNode root = new TreeNode(rootVal);
//The position of the current node in the inorder sequence
int indexInOrder = map.get(rootVal);
//右子树的长度
int rightLen = rightIn-indexInOrder;
//rightIn为啥是-1,Because it is from right to left
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;
}

边栏推荐
- 美摄问答室|美映 VS 美摄云剪辑
- MySQL 45 讲 | 10 MySQL为什么有时候会选错索引?
- winform 在Datatable插入一笔数据
- Using .NET to simply implement a high-performance clone of Redis (2)
- MATLAB程序设计与应用 3.1 特殊矩阵
- 【Inspirational】The importance of review
- Win11文件类型怎么改?Win11修改文件后缀的方法
- *W3C* 标准组织
- ECCV 2022 | 清华&腾讯AI Lab提出REALY: 重新思考3D人脸重建的评估方法
- zabbix deployment
猜你喜欢

Digital management insight into retail and e-commerce operations - retail password

热成像测温的原理是什么呢?你知道吗?

Win11文件类型怎么改?Win11修改文件后缀的方法

图文手把手教程--ESP32 OTA空中升级(VSCODE+IDF)

数字知识库及考学一体化平台

复盘:经典的HR面试问题,这些问题可以挖掘你个人的素质,看看你是否合适合我们部门

Zikko上市同时搭载HDMI2.1和2.5GbE新款雷电4扩展坞

上帝空间——全球首个基于Web3.0的艺术协议创意平台,拓宽多元艺术融合边界

【LeetCode】98.验证二叉搜索树

剑指长城炮? 长安全新皮卡官方谍照
随机推荐
Graphical Hands-on Tutorial--ESP32 OTA Over-the-Air Upgrade (VSCODE+IDF)
小程序实战(三) - head组件的封装与使用
数据化管理洞悉零售及电子商务运营——零售密码
What is the principle of thermal imaging temperature measurement?Do you know?
Super Learning Method
Win11怎么重装显卡驱动程序?Win11显卡驱动怎么卸载重装?
Xilinx VIVADO 中 DDR3(Naive)的使用(2)读写设计
Mysql高级篇学习总结13:多表连接查询语句优化方法(带join语句)
小程序实战(一)- 骨架屏的应用与实现
你知道吗?那些专属于代码的浪漫~
Win11文件类型怎么改?Win11修改文件后缀的方法
datax oracle to oracle incremental synchronization
Difference between ArrayList and LinkedList
中介者模式(Mediator)
vscode插件设置——Golang开发环境配置
JUC (1) threads and processes, concurrency and parallelism, thread state, locks, producers and consumers
使用json-server快速搭建本地数据接口
深度强化学习与APS的一些感想
Mysql——》类型转换符binary
剑指长城炮? 长安全新皮卡官方谍照