当前位置:网站首页>Search tree judgment (25 points)
Search tree judgment (25 points)
2022-04-23 08:41:00 【Huaihua second affectionate】
For binary search trees , We stipulate that the left subtree of any node contains only the key values which are strictly smaller than the node , And its right subtree contains the key value greater than or equal to the node . If we swap the left and right subtrees of each node , The resulting tree is called a mirror binary search tree .
Now let's give a sequence of integer key values , Please write a program to judge whether the sequence is a preorder traversal sequence of a binary search tree or a mirror binary search tree , If it is , Then output the post order traversal sequence of the corresponding binary tree .
Input format :
The first line of input contains a positive integer N(≤1000), The second line contains N It's an integer , For the given sequence of integer key values , Numbers are separated by spaces .
Output format :
The first line of the output first gives the judgment result , If the input sequence is the preorder traversal sequence of a binary search tree or a mirror binary search tree , The output YES
, No side output NO
. If it turns out that YES
, The next line outputs the post order traversal sequence of the corresponding binary tree . Numbers are separated by spaces , But there can't be extra spaces at the end of the line .
sample input 1:
7
8 6 5 7 10 8 11
sample output 1:
YES
5 7 6 8 11 10 8
sample input 2:
7
8 6 8 5 10 9 11
sample output 2:
NO
#include<cstdio>
#include<vector>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
int g[1010];
vector<int>kp;
int n;
bool f;
void build(int l,int r){
if(l>r)return;
int i=r,j=l+1;
int t=g[l];
if(f){
while(g[i]>=t&&i>l)i--;
while(g[j]<t&&j<=r)j++;
}
else {
while(g[i]<t&&i>l)i--;
while(g[j]>=t&&j<=r)j++;
}
if(j-i!=1)return;
build(l+1,i);
build(j,r);
kp.push_back(t);
}
int main(){
cin>>n;
for(int i=0;i<n;i++){
cin>>g[i];
}
build(0,n-1);
if(kp.size()!=n){
f=!f;
build(0,n-1);
}
if(kp.size()==n){
cout<<"YES\n";
cout<<kp[0];
for(int i=1;i<n;i++){
cout<<" "<<kp[i];
}
}
else cout<<"NO\n";
return 0;
}
版权声明
本文为[Huaihua second affectionate]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230835222725.html
边栏推荐
- Swagger document export custom V2 / API docs interception
- Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
- MATLAB 画五星红旗
- Overview of bus structure
- How browser works
- Shell脚本进阶
- idea底栏打开services
- Excle plus watermark
- Queue (C language / linked list)
- Notes on 30 steps of introduction to Internet of things of yangtao electronics STM32 III. Explanation of new cubeide project and setting
猜你喜欢
cadence的工艺角仿真、蒙特卡洛仿真、PSRR
K210学习笔记(二) K210与STM32进行串口通信
After a circle, I sorted out this set of interview questions..
'恶霸' Oracle 又放大招,各大企业连夜删除 JDK。。。
Notes on 30 steps of introduction to the Internet of things of yangtao electronics STM32 III. cubemx graphical programming and setting the IO port on the development board
pgsql想实现mysql一样样的列子查询操作
Input / output system
正点原子携手OneOS直播 OneOS系统教程全面上线
Star Trek强势来袭 开启元宇宙虚拟与现实的梦幻联动
2022-04-22 OpenEBS云原生存储
随机推荐
Queue (C language / linked list)
DOM 学习之—添加+-按钮
Use of applicationreadyevent
GUI编程简介 swing
汇编语言与逆向工程 栈溢出漏洞逆向分析实验报告
RPC procedure
PgSQL wants to implement all kinds of column sub query operations of MySQL
Yangtao electronic STM32 Internet of things entry 30 step notes II. Cube ide download, installation, sinicization and setting
IDEA导入commons-logging-1.2.jar包
How to generate assembly file
php基于哈希算法出现的强弱比较漏洞
uni-app和微信小程序中的getCurrentPages()
Let the earth have less "carbon" and rest on the road
使用flask和h5搭建网站/应用的简要步骤
Go语言自学系列 | golang结构体的初始化
xctf刷题小记
Navicat remote connection MySQL
Input / output system
还原二叉树 (25 分)
ELK生产实践