//============================================================================
// Name        : ReconstructTree.cpp
// Author      : Ken
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <vector>
#include <sstream>
#include <map>
using namespace std;

class Node{
public:
	int id;
	Node* p_parent;
	Node* p_left_child;
	Node* p_right_child;

	Node(): id(-1), p_parent(NULL), p_left_child(NULL), p_right_child(NULL){};
	Node(const int n):id(n), p_parent(NULL), p_left_child(NULL), p_right_child(NULL){}
};

void initializeMap(int n, map<int, Node>& nodeMap, map<int, int>& preOrderMap, map<int, int>& preOrderMapInv){
	for(int i=0; i<n; i++){
		int input;
		cin >> input;
		nodeMap[input] = Node(input);
		preOrderMap[i] = input;
		preOrderMapInv[input] = i;
	}
}

void readInOrderList(int n, map<int, int>& inOrderMap, map<int, int>& inOrderMapInv){
	for(int i=0; i<n; i++){
		int input;
		cin >> input;
		inOrderMap[i] = input;
		inOrderMapInv[input] = i;
	}
}

void reconstructTree(map<int, Node>& nodeMap,
		             map<int, int>& preOrderMap, map<int, int>& preOrderMapInv, int pre_start, int pre_end,
		             map<int, int>& inOrderMap, map<int, int>& inOrderMapInv, int in_start, int in_end){
	if(pre_start == pre_end - 1){
		return;
	}

	int leftMaxPos = pre_start;
	Node& root = nodeMap[preOrderMap[pre_start]];
    int rootPosInOrder = inOrderMapInv[preOrderMap[pre_start]];

	if(preOrderMap[pre_start] != inOrderMap[in_start]){
	    Node& left_child = nodeMap[preOrderMap[pre_start + 1]];
	    root.p_left_child = &left_child;
	    //cout << "root: " << root.id << "\n";
	    left_child.p_parent = &root;
	    //cout << "left_child: " << left_child.id << "\n";


	    for(int i=in_start; i<rootPosInOrder; i++){
	    	int position = preOrderMapInv[inOrderMap[i]];
		    if(position > leftMaxPos){
			    leftMaxPos = position;
		    }
	    }
		//cout << "reconstructTree " << pre_start+1 << " " << leftMaxPos+1 << " " << in_start << " " << rootPosInOrder << "\n";
		reconstructTree(nodeMap,
	                    preOrderMap, preOrderMapInv, pre_start+1, leftMaxPos+1,
						inOrderMap, inOrderMapInv, in_start, rootPosInOrder);
	}

	if(preOrderMap[pre_start] != inOrderMap[in_end-1]){
	    Node& right_child = nodeMap[preOrderMap[leftMaxPos+1]];
	    root.p_right_child = &right_child;
	    right_child.p_parent = &root;
	//cout << "right_child: " << right_child.id << "\n";


	//cout << "reconstructTree " << leftMaxPos+1 << " " << pre_end << " " << rootPosInOrder+1 << " " << in_end << "\n";
	    reconstructTree(nodeMap,
			        preOrderMap, preOrderMapInv, leftMaxPos+1, pre_end,
					inOrderMap, inOrderMapInv, rootPosInOrder+1, in_end);
	}
}

void writeMap(map<int, int> int_map){
	for(pair<int, int> element: int_map){
		cout << element.first << "->" << element.second << "\n";
	}
	cout << "\n";
}

void writeMap(map<int, Node> nodeMap){
	for(pair<int, Node> element:nodeMap){
		Node& node = element.second;
		int parent = -1, left_child = -1, right_child = -1;
		if(node.p_parent != NULL){
			parent = node.p_parent->id;
		}
		if(node.p_left_child != NULL){
			left_child = node.p_left_child->id;
		}
		if(node.p_right_child != NULL){
			right_child = node.p_right_child->id;
		}

		//cout << "id:" << node.id << " parent:" << parent << " left_child:" << left_child
		//	 << " right_child:" << right_child << "\n";
	}
}

void writePostOrder(Node& node, stringstream& ss){
	if(node.p_left_child != NULL){
	    writePostOrder(*(node.p_left_child), ss);
	}

	if(node.p_right_child != NULL){
	    writePostOrder(*(node.p_right_child), ss);
	}
	ss << node.id << " ";
}

int main() {
	int n;
	cin >> n;
	map<int, Node> nodeMap;
	map<int, int> preOrderMap;
	map<int, int> preOrderMapInv;
	map<int, int> inOrderMap;
	map<int, int> inOrderMapInv;

	//cout << "initializeMap\n";
    initializeMap(n, nodeMap, preOrderMap, preOrderMapInv);
    //cout << "readInOrderList\n";
	readInOrderList(n, inOrderMap, inOrderMapInv);

	//writeMap(preOrderMap);
	//writeMap(preOrderMapInv);
	//writeMap(inOrderMap);
	//writeMap(inOrderMapInv);

	//cout << "reconstructTree\n";
	reconstructTree(nodeMap, preOrderMap, preOrderMapInv, 0, n, inOrderMap, inOrderMapInv, 0, n);
	//writeMap(nodeMap);

	stringstream ss;
	Node& root = nodeMap[preOrderMap[0]];


	writePostOrder(root, ss);
	string str = ss.str();
	cout << str.substr(0, str.size()-1) << "\n";

	return 0;
}



Measured memory_bytes: 3220