当前位置:网站首页>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;
}边栏推荐
- Basic syntax of MySQL DDL and DML and DQL
- Write code, and multiple characters move from both ends to converge in the middle
- Mitsubishi Ethernet module Yuanchuang intelligent control yc8000-fx connection MCGS operation method
- 会话技术相关
- Cache component status when Vue components are switched, that is, dynamic components keep alive dynamic components and asynchronous components
- Android开发——Kotlin语法之Lambda表达式
- Do you know how to do interface testing well?
- The local picture cannot be displayed after the uniapp H5 is packaged
- B. Eastern Exhibition- Codeforces Round #703 (Div. 2)
- 4.合宙Air32F103_LCD
猜你喜欢

Worthington lysozyme technical description and literature reference
![Embedded system transplantation [5] - Cross compilation tool chain](/img/2a/eadaaafe794aa9b3106441fa50ffc7.png)
Embedded system transplantation [5] - Cross compilation tool chain

Lagrange polynomial

一篇搞定CAS,深度讲解,面试实践必备
![Scenario and value of data desensitization [summary]](/img/15/ebfbbb708c94417e7291941e76b3a3.png)
Scenario and value of data desensitization [summary]

Write code, and multiple characters move from both ends to converge in the middle

MySQL学习——MySQL软件的安装及环境配置(Windows)详细!

QT ROS related operations (running Terminal instructions, publishing and subscribing to custom message topics or services, subscribing to images and displaying)

PAT甲级 1041 Be Unique

Why do some people write code so complicated?
随机推荐
The local picture cannot be displayed after the uniapp H5 is packaged
RSA of go language parses jsencrypt with secret key JS the encrypted ciphertext of this library failed
Batch visual target detection callout box -- Yolo format dataset
PAT甲级 1041 Be Unique
An in-depth explanation of CAS is necessary for interview practice
Embedded system transplantation [5] - Cross compilation tool chain
Android Development - lambda expression of kotlin syntax
三菱转以太网模块远创智控YC8000-FX 连接 MCGS操作方法
Prosci Lag3 antibody: improve in vitro research and help cancer immunotherapy
1. Learn to understand web pages
Istio architecture extension mechanism
错误代码0x80004005
数据湖(十九):SQL API 读取Kafka数据实时写入Iceberg表
Matlab Simulink simulation of lithium iron phosphate battery
俄罗斯方块、1
Active vibration reduction system of hub motor and its vertical performance optimization
STL multimap
Read and understand the advantages of the LAAS scheme of elephant swap
MLP - Multilayer Perceptron
Introduction to pytorch ecology
https://github.com/ZouJiu1/PAT