当前位置:网站首页>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;
}边栏推荐
- 08 reptile project
- You must know the ten vulnerabilities of invalid access control
- Worthington: characteristics and other parameters of hexokinase from yeast
- Anchor point and anchor frame of target detection
- How emqx 5.0 under the new architecture of mria + rlog realizes 100million mqtt connections
- DOM相关的方法概念
- Internet of things installation and debugging personnel let "smart" life come early
- 93. (leaflet chapter) leaflet situation plotting - modification of attack direction
- Idea failed to load resource: the server responded with a status of 404 (not found)
- buu web
猜你喜欢

Paper reading: the perfect match: 3D point cloud matching with smoothed densities

IO stream sorting

Programmers may still be programmers, and coders may only be coders

idea写web项目时报错Failed to load resource: the server responded with a status of 404 (Not Found)

Matlab sound signal processing frequency diagram signal filtering and playing sound

Matlab Fractional Order PID control

训练赛《眼不见,心不烦,理不乱》题解

Matlab Simulink simulation of lithium iron phosphate battery

MLP - Multilayer Perceptron

Technical dry goods | how difficult is data processing? Take a look at the solution provided by mindspire!
随机推荐
Matlab Simulink simulation of lithium iron phosphate battery
Okaleido tiger NFT is about to log in to the binance NFT platform. Are you looking forward to it?
MySql的DDL和DML和DQL的基本语法
Jump statement in day011 loop structure
C language classic exercises (2) - "bubble sort"“
Realize the communication before two pages (using localstorage)
正则表达式 \b \B 深入浅出理解单词边界的匹配
Paper reading: the perfect match: 3D point cloud matching with smoothed densities
STL multimap
Genesis public chain: Tamp the foundation of Web 3.0 development
STL set容器
训练赛《眼不见,心不烦,理不乱》题解
Matlab ode45 solving differential equations
STL multimap
Android Development - lambda expression of kotlin syntax
Developers share the first chapter of "Book Eating bar: deep learning and mindspire practice"
Arduino interrupt realizes rising edge detection and executes other functions
Workbnech application of dynamixel steering gear under ROS
PAT甲级 1041 Be Unique
Binary search
https://github.com/ZouJiu1/PAT