当前位置:网站首页>Binary search tree from preorder to preorder and postorder
Binary search tree from preorder to preorder and postorder
2022-04-23 00:36:00 【hys__ handsome】
First , To understand that the preamble sequence of a binary search tree can uniquely determine a binary search tree .
First step , Establish root node ( The first value of the sequence )
The second step , Get the offset of the first node of the right subtree k( Used to divide the interval )
The third step , Divide the interval of left and right subtrees
#include<iostream>
#include<vector>
using namespace std;
typedef struct Node* Tree;
const int N = 1010;
vector<int> hx,qx,zx;
int a[N];
struct Node{
int data;
Tree l,r;
Node(int _data){
data = _data;
l = r = nullptr;
}
};
// l Is the beginning of the sequence ,sz Is the size of the sequence
void build(int l,int sz,Tree &tree){
if(sz <= 0) return;
tree = new Node(a[l]);
int k = 1;
while(l+k < sz && a[l+k] < a[l]) k++;
qx.push_back(tree->data);
build(l+1,k-1,tree->l);
zx.push_back(tree->data);
build(l+k,sz-k,tree->r);
hx.push_back(tree->data);
}
int main(){
int n;
cin>>n;
for(int i = 0; i < n; i++) cin>>a[i];
Tree root = nullptr;
build(0,n,root);
cout<<" The first sequence traversal :";
for(auto num:qx) cout<<num<<' ';
cout<<"\n In the sequence traversal :";
for(auto num:zx) cout<<num<<' ';
cout<<"\n After the sequence traversal :";
for(auto num:hx) cout<<num<<' ';
return 0;
}
/*
7
8 6 5 7 10 8 11
*/
版权声明
本文为[hys__ handsome]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230033479607.html
边栏推荐
- Nc13251 customer model
- MySQL -- operation of database
- 2.58 - write the program is little endian, which returns 1 when compiled and run on the small end method machine and 0 when compiled and run on the large end method machine. This program should be abl
- Symbolization of ArcGIS surface tin surface data
- ArcGIS 地表TIN面数据的符号化
- 湖泊遥感研究进展(概述)
- Differences of lake water color, water environment and hydrological remote sensing
- ArcGIS 城市生活区用地适宜性评价(一)
- Pain points solved by tidb under the wave of localization
- L2-021 点赞狂魔 (25 分)
猜你喜欢

多测师杭州拱墅校区肖sir_高级金牌讲师_简历制作讲解

idea中使用thymeleaf 模板 <img th:src=“${map.user.headerUrl}“ 报错Cannot resolve ‘user‘

ArcGIS TIN地表面与栅格地表面的生成与互相转换

ArcGIS 城市生活区用地适宜性评价(四)
![[classification de l'image] - Venez et séchez ce bol d'efficientnet](/img/74/8c811ba94e29947d5c2ae0e1c05131.png)
[classification de l'image] - Venez et séchez ce bol d'efficientnet

Beifu el6631 and Siemens 1200 do PROFINET master-slave communication

A tikv hard disk usage problem caused by GC not working caused by ticdc exception

MySQL -- data type

Steps to apply for a CA certificate

(to) excel 2016 does not have enough memory or disk space to open excel
随机推荐
群体智能协同作业与认知计算技术研究
Bypass network restrictions through RDP tunnel
Mitsubishi fx5u configures iebasic setting of mr-je-c servo driver
ArcGIS 制作3D规划图纸
湖泊的水色、水环境、水文遥感的区别
Mp2459 is a perfect replacement for 60v0 with power MOSFET fs2459 integrated inside 5A step-down IC
L2-007 家庭房产 (25 分) 并查集或图的遍历
关于软考,这些常见问题一定要明白
Luogu p2241 statistical square
ArcGIS urban living area land suitability evaluation (II)
L2-020 功夫传人 (25 分)
Difference between Beifu twincat1260 and tf5010 authorization
牛客NC13251模
L2-002 链表去重 (25 分) 标程
Migrate AWS S3 data to tidb cloud cluster
MP2459被完美替代内部集成有功率MOSFET管FS2459的60V0.5A降压IC
[AI vision · quick review of today's sound acoustic papers, issue 4] Thu, 21 APR 2022
接口文档生成工具ApiPost 绝了
L2-011 玩转二叉树 (25 分)
jsp 转换为thymeleaf格式的部分方式