当前位置:网站首页>PAT甲级 1043 Is It a Binary Search Tree
PAT甲级 1043 Is It a Binary Search Tree
2022-07-24 03:42:00 【九是否非随机的称呼】
先根据题意产生二叉树,找到小于或者大于rootnode的位置,并排除不符合条件的序列,
然后使用递归,后续遍历即可
#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;
}边栏推荐
- Android开发——Kotlin语法之Lambda表达式
- Arduino interrupt realizes rising edge detection and executes other functions
- Emqx v4.4.5 Publishing: new exclusive subscriptions and mqtt 5.0 publishing attribute support
- Using global data to realize data sharing in wechat applet
- Express内置的中间件
- C dynamic memory management details
- Write code, and multiple characters move from both ends to converge in the middle
- Okaleido tiger NFT is about to log in to the binance NFT platform. Are you looking forward to it?
- MySQL message queue list structure
- Matlab sound signal processing frequency diagram signal filtering and playing sound
猜你喜欢

buu web

Convert the pseudo array returned by childNodes into a true array

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

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

Keras deep learning practice (15) -- realize Yolo target detection from scratch

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

Why do some people write code so complicated?

IO流分类整理

Database foundation and installation

一篇搞定CAS,深度讲解,面试实践必备
随机推荐
How to realize WiFi Internet short message authentication in the park / factory
Expressions régulières \ \ B \ \ b compréhension de l'appariement des limites des mots
Mitsubishi Ethernet module Yuanchuang intelligent control yc8000-fx connection MCGS operation method
Redis
C user defined type details
Demining game (analysis)
Redis transaction learning
Idea failed to load resource: the server responded with a status of 404 (not found)
Developers share the first chapter of "Book Eating bar: deep learning and mindspire practice"
Master chip csu18m92 develops intelligent scale scheme
Machine learning notes - image homography estimation based on deep learning (homographynet)
Conteneur STL set
Convert the pseudo array returned by childNodes into a true array
Tetris, 1
Internet of things installation and debugging personnel let "smart" life come early
C file operation details
MySQL message queue list structure
Technical dry goods | how difficult is data processing? Take a look at the solution provided by mindspire!
Scenario and value of data desensitization [summary]
MySql的DDL和DML和DQL的基本语法
https://github.com/ZouJiu1/PAT