#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
vector<int> pre,in,post;
int n,pos;
void rec(int l,int r) {
if (l>=r) return ;
int root=pre[pos++]; //Determine the node
int m=distance(in.begin(), find(in.begin(),in.end(),root)); //The position of the early arrival node at in determines the position of the child node
post.push_back(root); //Press the post stack
}
void selove() {
pos=0;
rec(0,pre.size()); //The result of pre traversal determines the node order
for (int i=0;i<n;i++) {
if (i) cout<<' ';
cout<<post[i];
}
cout << endl;
}
int main() {
int k;
cin>>n;
for (int i=0;i<n;i++) {
cin>>k;
pre.push_back(k);
}
for (int i=0;i<n;i++) {
cin>>k;
in.push_back(k);
}
selove();
return 0;
}
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
vector<int> pre,in,post;
int n,pos;
void rec(int l,int r) {
if (l>=r) return ;
int root=pre[pos++]; //Determine the node
int m=distance(in.begin(), find(in.begin(),in.end(),root)); //The position of the early arrival node at in determines the position of the child node
rec(l,m); //Left node recursion
rec(m+1,r); // right node recursionpost.push_back(root); //Press the post stack
}
void selove() {
pos=0;
rec(0,pre.size()); //The result of pre traversal determines the node order
for (int i=0;i<n;i++) {
if (i) cout<<' ';
cout<<post[i];
}
cout << endl;
}
int main() {
int k;
cin>>n;
for (int i=0;i<n;i++) {
cin>>k;
pre.push_back(k);
}
for (int i=0;i<n;i++) {
cin>>k;
in.push_back(k);
}
selove();
return 0;
}