# XYNUOJ 进击的二叉查找树

## 样例输入

5
4 2 1 3 5
4 5 2 3 1

## 样例输出

YES
1 3 2 5 4
4 2 5 1 3

# include<stdio.h>
# include<queue>
using namespace std;
int n,flag=1;
struct node
{
int key;
node *  left;
node *  right;
node(int e):key(e),left(NULL),right(NULL){}
};
node * insert(node * r,int k)  //建立
{
if(r==NULL)
{
//  printf("###\n");
return new node (k);
}
if(r->key>k)
r->left=insert(r->left,k);
else
r->right=insert(r->right,k);
//printf("***\n");
return r;
}

bool cmp(node *r1,node *r2) //判断两棵树是不是相同
{
if(!r1&&!r2)
return true;
else
return r1->key==r2->key&&cmp(r1->left,r2->left)&&cmp(r1->right,r2->right);
return false;
}
void cch(node *r,int  k)   //层序遍历
{
queue<node *>q;
q.push(r);
while(!q.empty())
{   k++;
r=q.front();
q.pop();
if(k<n)
printf("%d ",r->key);
else printf("%d\n",r->key);
if(r->left)
q.push(r->left);
if(r->right)
q.push(r->right);
}
}
void pri(node * r,int k){ //后序遍历
if(r!=NULL)
{
k++;
pri(r->left,k);
pri(r->right,k);
if(k>1)
printf("%d ",r->key);
else printf("%d\n",r->key);
}
}

int main(){
scanf("%d",&n);
int c1;
node * r1=NULL;
for(int i=0;i<n;i++)
{
scanf("%d",&c1);
r1=insert(r1,c1);
}
node * r2=NULL;
for(int i=0;i<n;i++)
{
scanf("%d",&c1);
r2=insert(r2,c1);
}
if(cmp(r1,r2))
{
printf("YES\n");
}
else printf("NO\n");
pri(r1,0);
cch(r1,0);
return 0;
}