当前位置:网站首页>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
边栏推荐
猜你喜欢

MySQL查询两张表属性值非重复的数据

使用flask和h5搭建网站/应用的简要步骤

GUI编程简介 swing

CGM optimizes blood glucose monitoring and management -- Yiyu technology appears in Sichuan International Medical Exchange Promotion Association

STM32 uses Hal library. The overall structure and function principle are introduced

Harbor企业级镜像管理系统实战

在sqli-liabs学习SQL注入之旅(第十一关~第二十关)

STM32使用HAL库,整体结构和函数原理介绍

【深度好文】Flink SQL流批⼀体化技术详解(一)

'bully' Oracle enlarged its move again, and major enterprises deleted JDK overnight...
随机推荐
input元素添加监听事件
Knowledge points and problem solutions related to information collection
洋桃电子STM32物联网入门30步笔记三、CubeMX图形化编程、设置开发板上的IO口
How to generate assembly file
Use of Arthas in JVM tools
Notes on 30 steps of introduction to Internet of things of yangtao electronics STM32 III. Explanation of new cubeide project and setting
在sqli-liabs学习SQL注入之旅(第十一关~第二十关)
idea配置连接远程数据库MySQL,或者是Navicat连接远程数据库失败问题(已解决)
LaTeX论文排版操作
RPC过程
Add listening event to input element
dataBinding中使用include
【58】最后一个单词的长度【LeetCode】
Queue (C language / linked list)
STM32 uses Hal library. The overall structure and function principle are introduced
Reference passing 1
idea底栏打开services
Yangtao electronic STM32 Internet of things introduction 30 steps notes 1. The difference between Hal library and standard library
【IndexOf】【lastIndexOf】【split】【substring】用法详解
Use of applicationreadyevent