当前位置:网站首页>Pat grade a 1043 is it a binary search tree
Pat grade a 1043 is it a binary search tree
2022-07-24 03:45:00 【IX. is it a non random title】
First generate a binary tree according to the meaning of the topic , Find less than or greater than rootnode The location of , And exclude unqualified sequences ,
Then use recursion , Subsequent traversal is enough
#include<iostream>
#include<vector>
#include<bits/stdc++.h>
using namespace std;
typedef struct _binary{
int val=999999999;
_binary *l=NULL;
_binary *r=NULL;
}binary;
vector<int> pos;
bool res = true;
void generate_binary(vector<int> v, binary *root, int way){
vector<int> v0, v1, v0k;
root->val = v[0];
int i, j;
for(i = 1; i < v.size(); i++){
if(way==0&&v[i]>=v[0]) break;
else if(way==1&&v[i]<v[0]) break;
}
if(res==false) return;
if(way==0){
for(j = 1; j <i; j++){
if(v[j]>=v[0]){
res = false;
break;
}
}
for(j = i; j < v.size(); j++){
if(v[j] < v[0]){
res = false;
break;
}
}
}else{
for(j = 1; j <i; j++){
if(v[j] < v[0]){
res = false;
break;
}
}
for(j = i; j < v.size(); j++){
if(v[j] >= v[0]){
res = false;
break;
}
}
}
v0 = v;
v1 = v;
v0.erase(v0.begin() + i, v0.end());
v0.erase(v0.begin(), v0.begin()+1);
v1.erase(v1.begin(), v1.begin() + i);
if(v0.size()!=0){
root->l = (binary *)malloc(sizeof(binary));
root->l->l = nullptr;
root->l->r = nullptr;
generate_binary(v0, root->l, way);
}
if(v1.size()!=0){
root->r = (binary *)malloc(sizeof(binary));
root->r->l = nullptr;
root->r->r = nullptr;
generate_binary(v1, root->r, way);
}
}
// void checkout(binary *root, int way){
// int l, r, val;
// val = root->val;
// if(res==false)
// return;
// if(way==0){
// l = -999999999;
// r = 999999999;
// }else{
// r = -999999999;
// l = 999999999;
// }
// if(root->l!=nullptr)
// l = root->l->val;
// if(root->r!=nullptr)
// r = root->r->val;
// if(way==0){
// if(l >= val) res = false&&res;
// if(r < val) res = false&&res;
// if(l >= r) res = false&&res;
// }else{
// if(l < val) res = false&&res;
// if(r >= val) res = false&&res;
// if(l < r) res = false&&res;
// }
// res = true&&res;
// if(root->l!=nullptr) {
// checkout(root->l, way);
// }
// if(root->r!=nullptr) {
// checkout(root->r, way);
// }
// }
void postorder(binary *root){
if(root->l!=nullptr) postorder(root->l);
if(root->r!=nullptr) postorder(root->r);
pos.push_back(root->val);
}
int main(void){
int i, j, k, m, n, way;
cin>>m;
vector<int> v;
for(i=0; i<m; i++){
cin>>n;
v.push_back(n);
}
if(v[0] <= v[v.size()-1]) way = 0;
else way = 1;
binary *root = (binary *)malloc(sizeof(binary));
root->l = nullptr;
root->r = nullptr;
generate_binary(v, root, way);
// checkout(root, way);
if(res==false) {
cout<<"NO";
return 0;
}
postorder(root);
cout<<"YES"<<endl;
for(i = 0; i < pos.size(); i++){
cout<<pos[i];
if(i!=pos.size()-1) cout<<" ";
}
return 0;
}边栏推荐
- Scenario and value of data desensitization [summary]
- Data Lake (19): SQL API reads Kafka data and writes it to iceberg table in real time
- STL set容器
- Embedded system transplantation [5] - Cross compilation tool chain
- [JS reverse hundred examples] a public resource trading network, reverse analysis of announcement URL parameters
- [super complete sorting] Cisco and Huawei order comparison memo, take it away without thanks! Anytime, anywhere
- 复杂嵌套的对象池(5)——对象池的统一管理和拓展
- The progress in the stack will consume functions that cannot meet the needs of the enterprise. We are committed to
- IO流分类整理
- Idea failed to load resource: the server responded with a status of 404 (not found)
猜你喜欢

Expressions régulières \ \ B \ \ b compréhension de l'appariement des limites des mots

Lagrange polynomial

Leetcode-382. random nodes of linked list

Method sharing of saving data to CSV file in MATLAB

Appendtofile append failed

An in-depth explanation of CAS is necessary for interview practice

什么是IMU?

Matlab ode45 solving differential equations

Technical dry goods | evaluation index based on mindspire detailed perflexity language model

Realize the communication before two pages (using localstorage)
随机推荐
正则表达式 \b \B 深入浅出理解单词边界的匹配
DOM相关的方法概念
Rpc-bdy (5) - automatic service logoff, load balancing
DOM related method concepts
Write code, and multiple characters move from both ends to converge in the middle
IO stream sorting
Qt ROS相关操作(运行终端指令、发布订阅自定义消息话题或服务、订阅图像并显示)
Worthington hydroxysteroid dehydrogenase technical description and determination scheme
buu web
Matlab Simulink hydropower and synchronous motor power generation
Two stroke engine mean value model simulation
Demining game (analysis)
Jump statement in day011 loop structure
Machine learning notes - image homography estimation based on deep learning (homographynet)
Skywalking distributed system application performance monitoring tool - upper
Batch visual target detection callout box -- Yolo format dataset
An in-depth explanation of CAS is necessary for interview practice
The progress in the stack will consume functions that cannot meet the needs of the enterprise. We are committed to
Introduction to pytorch ecology
Binary search
https://github.com/ZouJiu1/PAT