#include <iostream>
#include <queue>
using namespace std;
int Pre[30],In[30];
typedef struct BinaryTree{
int key;
BinaryTree *left;
BinaryTree *right;
}BinaryTree,*Tree;
//
int FindRootInInoder(int start,int end,int key){
for(int i=start;i<end;i++){
if(In[i]==key)
return i;
}
}
//重构二叉树
BinaryTree *getBinaryTree(int pstart,int pend,int istart,int iend){
//初始化根节点
BinaryTree *Root = new struct BinaryTree;
*Root = {Pre[pstart],NULL,NULL};
//找中序遍历的根节点位置r
int r = FindRootInInoder(istart,iend,Pre[pstart]);
int count = r-istart;//左子树节点个数
if(r!=istart)
Root->left = getBinaryTree(pstart+1,pstart+count,istart,r-1);
if(r!=iend)
Root->right = getBinaryTree(pstart+1+count,pend,r+1,iend);
return Root;
}
//镜像二叉树
Tree change(Tree root){
Tree t;
if(root){
if(root->left!=NULL||root->right!=NULL){
t=root->left;
root->left=root->right;
root->right=t;
}
change(root->left);
change(root->right);
}
return root;
}
//输出层序遍历
void Sequence(Tree root){
queue<Tree> t;
if(root)
t.push(root);
while(!t.empty()){
root=t.front();
t.pop();
cout<<root->key;
if(root->left)
t.push(root->left);
if(root->right)
t.push(root->right);
if(!t.empty())
cout<<" ";
}
}
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++)
cin>>In[i];
for(int i=0;i<n;i++)
cin>>Pre[i];
Tree root = getBinaryTree(0,n-1,0,n-1);//重构二叉树
change(root);//镜像二叉树
Sequence(root);//层序输出
return 0;
}