这是二叉搜索树吗?
一棵二叉搜索树可被递归地定义为具有下列性质的二叉树:对于任一结点,
其左子树中所有结点的键值小于该结点的键值;
其右子树中所有结点的键值大于等于该结点的键值;
其左右子树都是二叉搜索树。
所谓二叉搜索树的“镜像”,即将所有结点的左右子树对换位置后所得到的树。
给定一个整数键值序列,现请你编写程序,判断这是否是对一棵二叉搜索树或其镜像进行前序遍历的结果。
输入格式:
输入的第一行给出正整数 N(≤1000)。随后一行给出 N 个整数键值,其间以空格分隔。
输出格式:
如果输入序列是对一棵二叉搜索树或其镜像进行前序遍历的结果,则首先在一行中输出 YES ,然后在下一行输出该树后序遍历的结果。数字间有 1 个空格,一行的首尾不得有多余空格。若答案是否,则输出 NO。
输入样例 1:
7
8 6 5 7 10 8 11
输出样例 1:
YES
5 7 6 8 11 10 8
输入样例 2:
7
8 10 11 8 6 7 5
输出样例 2:
YES
11 8 10 7 5 6 8
输入样例 3:
7
8 6 8 5 10 9 11
输出样例 3:
NO
我的做法比较“原始”,先把给出的数组看作就是“二叉搜索树的前序遍历”或“二叉搜索树镜像的前序遍历”(这里镜像的做法就是把左右子树反着建),然后前序遍历,若两棵树的前序遍历结果有一颗与所给数组顺序完全一致,则输出“YES”,并输出后序遍历结果,否则输出“NO”。
#pragma GCC optimize(3,"Ofast","inline")
#pragma G++ optimize(3)
#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef queue<int> q_i;
typedef queue<string> q_s;
typedef queue<double> q_d;
typedef queue<ll> q_ll;
typedef queue<char> q_c;
typedef priority_queue<int> pq_i;
typedef priority_queue<string> pq_s;
typedef priority_queue<double> pq_d;
typedef priority_queue<ll> pq_ll;
typedef stack<int> s_i;
typedef stack<string> s_s;
typedef stack<double> s_d;
typedef stack<ll> s_ll;
typedef stack<char> s_c;
typedef map<ll,ll> m_ll_ll;
typedef map<int,ll> m_i_ll;
typedef map<int,int> m_i_i;
typedef map<string,ll> m_s_ll;
typedef map<char,int> m_c_i;
typedef map<char,ll> m_c_ll;
const ll INF=0x3f3f3f3f;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define per(i,l,r) for(ll i=r;i>=l;i--)
#define eif else if
#define N 100005
#define mm(dp) memset(dp,0,sizeof(dp))
#define mm1(dp) memset(dp,-1,sizeof(dp))
#define mm2(dp) memset(dp,0x3f,sizeof(dp))
#define IT set<int>::iterator
#define fs(n) fixed<< setprecision(n)
//const double e=2.71828182845;
#define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b
const double pi = acos(-1.0);
inline ll read()
{
ll s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')
w=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
s=s*10+ch-'0',ch=getchar();
return s*w;
}
inline void write(ll x)
{
if(x<0)
putchar('-'),x=-x;
if(x>9)
write(x/10);
putchar(x%10+'0');
}
int a[1005];
int n;
struct node
{
int num;
struct node *l,*r;
};
struct node *head,*p,*t,*q,*head0;
int u=0;
int v=0;
int v1=0;
int c[1005];
int d[1005];
int b[1005];
void create1(int nm,struct node* &head1)
{
if(head1==NULL)
{
p=(struct node *)malloc(sizeof(struct node));
p->num=a[nm];
p->l=NULL;
p->r=NULL;
head1=p;
return;
}
else
{
if(a[nm]<head1->num)
create1(nm,head1->l);
else
create1(nm,head1->r);
}
}
void create2(int nm,struct node* &head1)
{
if(head1==NULL)
{
p=(struct node *)malloc(sizeof(struct node));
p->num=a[nm];
p->l=NULL;
p->r=NULL;
head1=p;
return;
}
else
{
if(a[nm]<head1->num)
{
create2(nm,head1->r);
}
else
create2(nm,head1->l);
}
}
void qian(struct node *t1)
{
if(t1==NULL)
return;
u++;
c[u]=t1->num;
qian(t1->l);
qian(t1->r);
}
void qian1(struct node *t1)
{
if(t1==NULL)
return;
v++;
d[v]=t1->num;
qian1(t1->l);
qian1(t1->r);
}
void hou(struct node *t1)
{
if(t1==NULL)
return;
hou(t1->l);
hou(t1->r);
v1++;
b[v1]=t1->num;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
rep(i,1,n)
{
cin>>a[i];
}
p=(struct node *)malloc(sizeof(struct node));
p->num=a[1];
head=p;
head->l=NULL;
head->r=NULL;
rep(i,2,n)
{
create1(i,head);
}
qian(head);
q=(struct node *)malloc(sizeof(struct node));
q->num=a[1];
head0=q;
head0->l=NULL;
head0->r=NULL;
rep(i,2,n)
{
create2(i,head0);
}
qian1(head0);
int flag=0;
int flag1=0;
rep(i,1,n)
{
if(a[i]!=c[i])
{
flag=1;
break;
}
}
rep(i,1,n)
{
if(a[i]!=d[i])
{
flag1=1;
break;
}
}
if(flag==1&&flag1==1)
{
cout<<"NO"<<endl;
}
else
{
cout<<"YES"<<endl;
if(flag==0)
{
hou(head);
cout<<b[1];
rep(i,2,n)
{
cout<<" "<<b[i];
}
cout<<endl;
}
else
{
hou(head0);
cout<<b[1];
rep(i,2,n)
{
cout<<" "<<b[i];
}
cout<<endl;
}
}
return 0;
}