#include <iostream>
#include <vector>
using namespace std;
struct node
{
int data;
node *left,*right;
};
void insert(node* &root,int data)//生成二叉搜索树
{
if(root==NULL)
{
root=new node;//二叉树初始化
root->data=data;
root->left=NULL;
root->right=NULL;
return ;
}
else
{
if(data>=root->data)//不要忘记=号
insert(root->right,data);
else
{
insert(root->left,data);
}
}
}
void preorder(node* root,vector<int>&v)//先序遍历一颗二叉树并存到数组中
{
if(root==NULL)
return ;
v.push_back(root->data);
preorder(root->left,v);
preorder(root->right,v);
}
void preorderM(node* root,vector<int>&v)//先序遍历二叉树的镜像
{
if(root==NULL)
return ;
v.push_back(root->data);
preorderM(root->right,v);
preorderM(root->left,v);
}
void postorder(node* root,vector<int>&v)//得到二叉树的后序遍历结果
{
if(root==NULL)
return ;
postorder(root->left,v);
postorder(root->right,v);
v.push_back(root->data);
}
void postorderM(node* root,vector<int>&v)//得到镜像的后序遍历结果
{
if(root==NULL)
return ;
postorderM(root->right,v);
postorderM(root->left,v);
v.push_back(root->data);
}
vector<int> bst,bst1,bst2,bst3,bst4;
int main()
{
int n,d;
node* root=NULL;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>d;
bst.push_back(d);
insert(root,d);
}
preorder(root,bst1);
preorderM(root,bst2);
postorder(root,bst3);
postorderM(root,bst4);
if(bst==bst1)//给整数定序列与二叉搜索树的先序遍历相同
{
cout<<"YES"<<endl;
for(int i=0;i<n-1;i++)
cout<<bst3[i]<<' ';//输出后序遍历
cout<<bst3[n-1]<<endl;
}
else if(bst==bst2)//给整数定序列与二叉搜索树镜像的先序遍历相同
{
cout<<"YES"<<endl;
for(int i=0;i<n-1;i++)
cout<<bst4[i]<<' ';
cout<<bst4[n-1]<<endl;
}
else{
cout<<"NO"<<endl;
}
return 0;
}