当前位置:网站首页>[jzof] 07 rebuild binary tree
[jzof] 07 rebuild binary tree
2022-07-23 13:19:00 【Sighed, angry】

Here is a record of how the preorder cutoff subscript of the left subtree is calculated . It's convenient to recall later :
Suppose this subscript is X. So there's the following equation :
i - inStart = X - preStart;
therefore :
X = preStart - inStart + i;
After this subscript is calculated , Other subscripts are simple .
import java.util.*;
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
public class Solution {
public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
TreeNode root=reConstructBinaryTree(pre, 0, pre.length - 1, in, 0, in.length - 1);
return root;
}
// The former sequence traversal {1,2,4,7,3,5,6,8} And middle order ergodic sequence {4,7,2,1,5,3,8,6}
private TreeNode reConstructBinaryTree(int [] pre,int startPre,int endPre,int [] in,int startIn,int endIn) {
if(startPre>endPre||startIn>endIn)
return null;
TreeNode root=new TreeNode(pre[startPre]);
for(int i=startIn;i<=endIn;i++)
if(in[i]==pre[startPre]) {
root.left=reConstructBinaryTree(pre,startPre+1,startPre+i-startIn,in,startIn,i-1);
root.right=reConstructBinaryTree(pre,i-startIn+startPre+1,endPre,in,i+1,endIn);
break;
}
return root;
}
}
边栏推荐
- 力扣 剑指 Offer II 094. 最少回文分割
- Beifu PLC and C transmit int type variables through ads communication
- Complex networks - common drawing software and libraries
- 第十二天笔记
- Beifu PLC and C transmit string array type variables through ads communication
- C language - big end storage and small end storage
- OpenCV图像处理(下) 边缘检测+模板匹配+霍夫变换
- Quelle est la raison pour laquelle la plate - forme easygbs ne peut pas lire l'enregistrement vidéo et a un phénomène de streaming répété rtmp?
- 第十天笔记
- Redis如何实现持久化?详细讲解RDB的三种触发机制及其优缺点,带你快速掌握RDB
猜你喜欢

Complex networks - common drawing software and libraries

Pod topology constraints

根据不同时间统计不同类型的数据(存储过程)

第十二天笔记

Successful joint commissioning of Vientiane Aoke and CoDeSys Technology

基于redis+lua进行限流

第十一天笔记

功能测试转自动化测试,十年自动化测试经验分享

OpenCV图像处理(下) 边缘检测+模板匹配+霍夫变换

How does redis implement persistence? Explain in detail the three triggering mechanisms of RDB and their advantages and disadvantages, and take you to quickly master RDB
随机推荐
【日常训练】814. 二叉树剪枝
功能测试转自动化测试,十年自动化测试经验分享
Beifu and C transmit real type through ads communication
Uncaught (in promise) Neo4jError: WebSocket connection failure. Due to security constraints in your
Beifu PLC and C # regularly refresh IO through ads communication
太空射击 Part 1: 玩家精灵和控制
信号完整性(SI)电源完整性(PI)学习笔记(三十一)电源分配网路(三)
Beifu PLC and C transmit bool type variables through ads communication
Beifu PLC and C transmit structure type variables through ads communication
转行软件测试有学历要求吗?低于大专是真的没出路吗?
倍福和C#通过ADS通信传输Real类型
Evaluation of classification model
Offer II 094. Minimum palindrome segmentation
Li Kou 729. My schedule I
redis分布式锁实践
Image processing image feature extraction and description
倍福PLC和C#通过ADS通信传输Bool数组变量
Space shooting part 2-2: enemy spirit
太空射击 Part 2-2: 敌人精灵
信號完整性(SI)電源完整性(PI)學習筆記(三十二)電源分配網路(四)