////////////////////////////////////////
//ALDS1_7_B:   Binary Trees
//ALDS1_7_C:   Tree Walk
//ALDS1_7_D:   Reconstruction of a Tree
//D???????????°??£????????±????§£?????§?????\???for?????§?§£??????????????????????§£?????????
////////////////////////////////////////

#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;

const int N_MAX = 40;
vector<Node> node;
//vi printer;

int preorder[N_MAX+1], inorder[N_MAX+1];
int x2inindex[N_MAX+1]; //????????°x???inorder????????§???x2inindex[x]????????????????????¨?????????

int N; //num of node
int targetPreorderIndex = 1; //????????¨??????????????????preorder[x]???index??§??????x?????????

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;
    }
    
    static int findTopNodeFromInorder(int left, int right) { //inorder[left ... right]??????????????????????????????????????????????????¢????????????index?????????
        if( left > right ) return -1;
        
        int x = preorder[targetPreorderIndex];
        targetPreorderIndex++;
        
        //inorder???2????????°?????????????????????????????°????????????
        int leftChild = findTopNodeFromInorder(left, x2inindex[x]-1); //x?????????????????????????????°?????????
        int rightChild = findTopNodeFromInorder(x2inindex[x]+1, right); //x?????????????????????????????°?????????
        
        node[x].left = leftChild;
        node[x].right = rightChild;
        
        node[leftChild].parent = x;
        node[rightChild].parent = x;
        
        cout << x << (x==preorder[1] ? "\n" : " ");
        return x;
    }
};



int main(){ //?????????1???based index??§???
    cin >> N;
    rep(i,0,N+1) node.push_back(*new Node()); //node[0]????????????????????????
    
    rep(i,1,N+1) cin >> preorder[i];
    rep(i,1,N+1) { cin >> inorder[i]; x2inindex[ inorder[i] ] = i; }
    
//        //test display
//        dispAll(preorder, N+1);
//        dispAll(inorder, N+1);
//        dispAll(x2inindex, N+1);
    
    //reconstruct the tree
    Node::findTopNodeFromInorder(1,N);
    
//    //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: 3084