当前位置:网站首页>Binary tree related creation or traversal
Binary tree related creation or traversal
2022-04-21 17:06:00 【Ximu fengluo】
One 、 Through the first root traversal and the middle root traversal of the array , Build and restore binary tree
thought : First root traversal array , The first element will be the root of the binary tree , Then find the position of the root in the root traversal , Then the child on the left of the middle root traversal is the left child , On the right is the right child . Using the idea of recursion .
public Node create(int[] pre, int[] in, int preStart, int preEnd, int inStart, int inEnd){
Node root = new Node(pre[preStart], null, null);
// Recursion end condition
if(preStart == preEnd && inStart==inEnd){
return root;
}
// Find the position of the root in the root traversal , Identify the left child and the right child
int ft = 0;
for(ft = inStart; ft < inEnd; ft++){
if(in[ft] == pre[preStart]){
break;
}
}
// Determine the number of left children
int left = ft - inStart;
int rigth = inEnd - ft;
// There are left children , Then recursively establish the left child
if(left > 0 ){
root.left = create(pre, in, preStart+1, preStart+left, inStart, ft -1);
}
if(right > 0){
root.right = create(pre, in, preStart+left+1, preEnd, ft+1, inEnd);
}
}
If it is followed by traversal and middle root traversal , The method is similar to , Mainly how to determine the relationship between the left child and the right child .
public Node create(int[] post, int[] in, int postStart, int postEnd, int inStart, int inEnd){
// Heel traversal , The last node is with
Node root = new Node(post[postEnd], null, null);
if(postStart == postEnd && inStart == inEnd){
return root;
}
// Find the midpoint
int ft = 0;
for(ft = inStart; ft < inEnd; ft++){
if(in[ft] == post[postEnd]){
break;
}
}
int left = ft - inStart;
int right = inEnd - ft;
if(left > 0){
root.left = create(post, in, postStart, postStart+left-1, inStart, ft -1);
}
if(right > 0){
root.right = create(post, in, postStart+left, postEnd-1, ft+1, inEnd);
}
}
版权声明
本文为[Ximu fengluo]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211702170558.html
边栏推荐
- Interpretation of a paper that points out the small errors in the classic RMS proof process
- Design and practice of unified security authentication for microservice architecture
- thrift简单应用
- R language data export (data saving, export and persistence to local specified directory file), write using xlsx package Xlsx function exports dataframe to excel file XLS format instead of xlsx format
- Discussion on next generation security protection
- R语言使用cor函数计算dataframe中多个数值数据列之间的相关性系数、计算Pearson积矩相关性系数
- mysql设置2个主键
- R语言使用Hmisc包的label函数为dataframe中的特定变量(数据列)添加变量标签、将dataframe的数据列名称修改为数据说明标签、而通过数值索引访问dataframe数据列
- 二叉树相关的创建或者遍历
- Control @ schedule on in different environments
猜你喜欢
随机推荐
SSD_ RESNET aircraft and oil drum data set actual combat
824. Goat Latin
ls -al各字段意思
pytorch index_ add_ Usage introduction
机器学习吴恩达课程总结(四)
Vitis HLS 构建项目并生成IP核(Vivado HLS)
mysql创建数据库sql语句
微服务架构统一安全认证设计与实践
数据库原理——图书馆管理系统
剑指 Offer II 011. 0 和 1 个数相同的子数组
R language uses qbern function to generate Bernoulli distribution (0-1 distribution) quantile function data, and uses plot function to visualize Bernoulli distribution
R语言使用grepl函数检查子字符串是否存在于指定的字符串中、字符串匹配,负责搜索给定字符串对象中是否包含特定表达式
The R language uses the plot function to visualize the data scatter diagram, and uses the BG parameter in the par function to customize the background color in the wireframe of the visual image
FastReport Business Graphics . NET
Vitis HLS build project and generate IP core (vivado HLS)
Golang binary analysis and reverse
不同特征程序类反弹shell
Vivado verifies the IP core generated by Vitis HLS
机器学习吴恩达课程总结(五)
前五章内容思维导图








