#include <iostream>
#include <algorithm> // next_permutation
#include <iomanip>
#include <cmath>
#include <vector>
#include <sstream>
#include <string>
#include <cstdio>
#include <stack>
#include <queue>
#include <list>
#include <numeric> //accumulate
//#include <unordered_map> //???????????\??¢??°
#include <fstream> //ifstream, ofstream

//#define NDEBUG //NDEBUG???#include <cassert>???????????????????????????????????´???assert?????????????????????????????????NDEBUG?????????????????????????????????????????????
#include <cassert> //assert


using namespace std;

//???????????°??????TEST????????????????????¬???????????????????????????????????¢????????????
#define TEST //*******************************************************************************************************************************************
//?????????????????¨??§?????\????????????????????????????????°?????????????¨???????????????????dout???????????????????????§???????????¬??????????????????????????????????????????
//??????????????????????????????????????????????????????????§?CPU???????£??????????????????§???TLE?????????????????????????????????????????§????????¨???                           ????????????????????????cerr????????£????????????????????????????????????
#ifdef TEST
#define dout cout
#else
stringstream dummy; //???????????°??????dout?????????????????????????????????
#define dout dummy.str(""); dummy.clear(stringstream::goodbit); dummy //dummy?????????????????????????????????????????¨?????¢?????¢???????????????
//???????????¨??????????????????goodbit?????????????????????????????¨???????????´????????????????????????????????¨?????°?????????????????§???????????????????????????????????? http://d.hatena.ne.jp/linden/20060427/p1
#endif

//?¨??????????????????????????????????????????????????????´??????OUTPUT2TEXTFILE????????????????????¬???????????????????????????????????¢????????????
//#define OUTPUT2TEXTFILE //*******************************************************************************************************************************************
#ifdef OUTPUT2TEXTFILE
#define dout outputfile //??¨????????????TLE????????§?????????dout?????¨??¨??????????????¢????????????????????????????????????????????§????¨????????????´??????????????£??????????????????????¨?????????????????????????????????????????????§?????????????????????
//TEST??????????????????????????¨???outputfile??????????????????????????????
#define OUTPUTFILENAME "output.txt"
ofstream outputfile(OUTPUTFILENAME);
#define OutputFilePath "/Users/Nag/Documents/Prgm/Test/DerivedData/Test/Build/Products/Debug/output.txt"
#endif

#define disp(A) dout << #A << " = " << (A) << endl
#define disP(A) dout << setw(3) << (A) << " " // << setw(3) ??????????????\????????????
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)

#define dispAll(A,n) dout << #A << " = "; rep(j, 0, (n)) {disP(A[j]);} dout << endl

typedef pair<int,int> pii;
typedef vector<int> vi;
typedef unsigned long long ll;

const int INF = 1e9-1;

class Node;

vector<Node> node;
vi printer;


class Node {
public:
    Node() {
        parent = -1;
        left = -1;
        right = -1;
    }
    
    int parent;
    int left;
    int right;
    
    
    int depth() {
        int p = this->parent;
        int d = 0;
        
        while( p != -1 ) {
            d++;
            p = node[p].parent;
        }
        
        return d;
    }
    
    static int height(int id) {
        if(id == -1) return -1;
        
        return max( height(node[id].left), height(node[id].right)) + 1;
    }
    
    static void printNodePreorder(int id) {
        if(id==-1) return;
        
        cout << " " << id;
        printNodePreorder(node[id].left);
        printNodePreorder(node[id].right);
    }
    
    static void printNodeInorder(int id) {
        if(id==-1) return;
        
        printNodeInorder(node[id].left);
        cout << " " << id;
        printNodeInorder(node[id].right);
    }
    
    static void printNodePostorder(int id) {
        if(id==-1) return;
        
        printNodePostorder(node[id].left);
        printNodePostorder(node[id].right);
        printer.push_back(id);
//        cout << " " << id;
    }
    
    
};



int main(){
    int N; cin >> N;
    rep(i,0,N+1) node.push_back(*new Node());
    
    //read input data
//    int p, c1, c2;
//    rep(i,0,N) {
//        //        dout<<"------";
//        //        disp(i);
//        cin >> p >> c1 >> c2;
//        
//        node[p].left = c1;
//        node[p].right = c2;
//        
//        if(c1!=-1) node[c1].parent = p;
//        if(c2!=-1) node[c2].parent = p;
//    }
    int preorder[N+1], inorder[N+1];
    int preindex2inindex[N+1]; //pre[i]=in[j]?????¨??????preindex2inindex[i]=j??¨??????????????????????????????in[j] = in[ preindex2inindex[i] ] = pre[i]
    bool registered[N+1]; //node[i]????????§????????????????????¨??????1????????????
    
    rep(i,1,N+1) cin >> preorder[i];
    rep(i,1,N+1) cin >> inorder[i];
    rep(i,1,N+1) registered[i] = 0;

    rep(i,1,N+1) { //preorder[i]???inorder[]????????????????????????????????????preindex2inindex[i]???????´???????
        int key = preorder[i];
        rep(j,1,N+1) {
            if(key == inorder[j]) {
                preindex2inindex[i] = j;
                break;
            }
        }
    }
    
//    //test display
//    dispAll(preorder, N+1);
//    dispAll(inorder, N+1);
//    dispAll(preindex2inindex, N+1);
//    dispAll(registered, N+1);

    
    //reconstruct tree
    //Preorder?????°???????????????????????????????????????????????????????????????
    //????????¨??????Inorder????????±????????????????????????????????????????±????????????????
    int x;
    
    //preorder[1] = root??¨?????????root?????????????????????????????¢?????????????????????????????????????????§?????±??´??°???????????????????????????????????°??????????????????????????????
    registered[ preorder[1] ] = 1;
    
    rep(i,2,N+1) { //?????????preorder[i]???????????????
        if( preindex2inindex[i] < preindex2inindex[i-1] ) { //inorder????????????preorder[i]???preorder[i-1]???????????´???????????°???preorder[i]???preorder[i-1]????????´????????¨???????????¨?????????
            node[ preorder[i-1] ].left = preorder[i];
            node[ preorder[i] ].parent = preorder[i-1];
            registered[preorder[i]] = 1; //????????????????????°????????????
        }
        else { //????????§????????£?????´???????????????????????´????????¨???????????????????????????????????¢??????????????????preorder??§????????¨??????????????????????????´????????????????????????????????????????????????????????§??????
            x = preindex2inindex[i] - 1; //x = inorder???????????????preorder[i]???1???????????°?????????index
            while( registered[ inorder[x] ]==0 ) x--; //inorder???????????????preorder[i]??????????????????????????§????????????????????????????????¢????????????????????????????????£???inorder[x]??????preorder[i]????????§??????????????´?????????preorder[i]??¨???????????¨?????????
            //??¶?????????????????????????????????????????????????????§??????????????????????????\???????????????????°?????????¨???root????????????????????????root?????????????????????????????????????????????????????????else?????¶?????\??£????????????????????????
            
            node[ inorder[x] ].right = preorder[i];
            node[ preorder[i] ].parent = inorder[x];
            registered[preorder[i]] = 1; //????????????????????°????????????
        }
        
//        //test display
//        dout<<"-----------------\n";
//        dout << "  i   l   r   p\n";
//        rep(i,0,N+1) {
//            disP(i);
//            disP(node[i].left); disP(node[i].right); disP(node[i].parent);
//            dout << endl;
//        }
    }
    
    
    
    
    
//    //output
//    rep(i,0,N+1) {
//        cout << "node " << i << ": parent = " << node[i].parent
//        << ", sibling = " << (node[i].parent==-1 ? -1 : (node[node[i].parent].left==i ? node[node[i].parent].right : node[node[i].parent].left) )
//        << ", degree = " << (node[i].left==-1 ? (node[i].right==-1 ? 0 : 1) : (node[i].right==-1 ? 1 : 2))
//        << ", depth = " << node[i].depth()
//        << ", height = " << Node::height(i) << ", "
//        << (node[i].parent==-1 ? "root" : (node[i].left==-1 && node[i].right==-1 ? "leaf" : "internal node") ) << endl;
//    }
    
    
    //find root
    int root_i = -1;
    rep(i,1,N+1) {
        if(node[i].parent==-1) {
            root_i = i;
            break;
        }
    }
    assert(root_i!=-1);
    
    //print tree
//    cout << "Preorder\n";
//    Node::printNodePreorder(root_i);
//    cout << endl;
//    
//    cout << "Inorder\n";
//    Node::printNodeInorder(root_i);
//    cout << endl;
//    
//    cout << "Postorder\n";
    Node::printNodePostorder(root_i);
    
    rep(i,0,N) {
        if(i==0) cout << printer[i];
        else cout << " " << printer[i];
    }
    cout << endl;
    
    
#ifdef OUTPUT2TEXTFILE
    outputfile.close();
    cout << "\"" << OutputFilePath << "\"" << endl;
#endif
    
    return 0;
}

Measured memory_bytes: 3144