#include <iostream>
struct node{
int data;
node *lchild,*rchild;
};
int pre[30],post[30],N;
bool ans=true;
node* createtree(int prel,int prer,int postl,int postr){
if(prel>prer) return NULL;
if(prel==prer){
node *root=new node;
root->data=pre[prel];
root->lchild=root->rchild=NULL;
return root;
}
if(pre[prel+1]==post[postr-1]) ans=false;
node* root=new node;
root->data=pre[prel];
int pos=prel+1;
for(;pos<=prer;pos++)
if(pre[pos]==post[postr-1]) break;
root->lchild=createtree(prel+1,pos-1,postl,postl+pos-1-prel-1);
root->rchild=createtree(pos,prer,postl+pos-1-prel,postr-1);
return root;
}
int printcount=0;
void inorder(node* root){
if(root==NULL) return;
inorder(root->lchild);
++printcount!=N?printf("%d ",root->data):printf("%d\n",root->data);
inorder(root->rchild);
}
int main(){
//freopen("g:\\in.txt","r",stdin);
scanf("%d",&N);
for(int i=0;i<N;i++)
scanf("%d",&pre[i]);
for(int i=0;i<N;i++)
scanf("%d",&post[i]);
node *root=NULL;
root=createtree(0,N-1,0,N-1);
if(ans) printf("Yes\n");
else printf("No\n");
inorder(root);
return 0;
}
PAT A1119
最新推荐文章于 2021-06-17 20:06:20 发布