Title:
Enter a positive integer N (<=1000), and then enter the serial number of N points. If the sequence just entered is a binary search tree or its mirror image (the center is flipped 180°), then output YES and output its post-order traversal, otherwise output NO.
trick:
for(auto it:post)
cout<
This output will make the 0th, 1st data point format wrong. . . The reason is unknown
Code:
#define HAVE_STRUCT_TIMESPEC
#include< bits/stdc++.h>
using namespace std;
int pre[1007];
vect or
int flag;
void dfs(int l,int r){
if(l>r)
return ;
int ll=l+1,rr=r;
if(!flag){
while(ll <=r&&pre[ll] ++ll;< br> while(rr>l&&pre[rr]>=pre[l])
–rr;
}
else{
while( ll<=r&&pre[ll]>=pre[l])
++ll;
while(rr>l&&pre[rr] –rr;
}
if(ll!=rr+1)
return ;
dfs(l+1,rr);
dfs(ll,r);
post.push_back(pre[l]);
}
int main(){
int n;
cin>>n;
for(int i=1;i<=n;++ i)
cin>>pre[i];
flag=0;
dfs(1,n);
if(post.size()
flag=1;
post.clear();
dfs(1,n );
}
if(post.size()< n)
cout<<"NO";
else{
cout< <"YES
"<
return 0;
}
cout<
for(int i=1;i
cout<<" "<
This is also correct