p02282 Reconstruction of a Tree_28508
Write a program which reads two sequences of nodes obtained by the preorder tree walk and the inorder tree walk on a binary tree respectively, and prints a sequence of the nodes obtained by the postorder tree walk on the binary tree.

Constraints

* $1 \leq n \leq 40$

Input

In the first line, an integer $n$, which is the number of nodes in the binary tree, is given.
In the second line, the sequence of node IDs obtained by the preorder tree walk is given separated by space characters.
In the second line, the sequence of node IDs obtained by the inorder tree walk is given separated by space characters.

Every node has a unique ID from $1$ to $n$. Note that the root does not always correspond to $1$.

Output

Print the sequence of node IDs obtained by the postorder tree walk in a line. Put a single space character between adjacent IDs.

Examples

Input

5
1 2 3 4 5
3 2 4 1 5


Output

3 4 2 5 1


Input

4
1 2 3 4
1 2 3 4


Output

4 3 2 1
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
import java.util.StringTokenizer;

public class Main {
	
	private class Node<T> {
		T value;
		Node<T> left;
		Node<T> right;
		public Node(T value) {
			this.value = value;
			left = right = null;
		}
		public String toString() {
			return value.toString();
		}
	}
	
	public static void main(String[] args) throws IOException {
		new Main().run();
	}
	
	private void run() throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(reader.readLine());
		int[] pre = new int[n];
		int[] in = new int[n];
		StringTokenizer st1 = new StringTokenizer(reader.readLine());
		StringTokenizer st2 = new StringTokenizer(reader.readLine());
		int rootId = 0;
		for (int i = 0; i < n; i++) {
			pre[i] = Integer.parseInt(st1.nextToken());
			in[i] = Integer.parseInt(st2.nextToken());
			if (i == 0) {
				rootId = pre[i];
			}
		}
		reader.close();
		
		Node<Integer> rootNode = new Node<Integer>(rootId);
		for (int i = 1; i < n; i++) {
			Node<Integer> current = new Node<Integer>(pre[i]);
			addNode(rootNode, current, in);
		}
		
		postOrder(rootNode);
	}

	private void postOrder(Node<Integer> rootNode) {
		StringBuilder sb = new StringBuilder();
		Stack<Node<Integer>> stack = new Stack<Node<Integer>>();
		Node<Integer> current = rootNode;
		Node<Integer> lastVisited = null;
		while (current != null || !stack.isEmpty()) {
			if (current != null) {
				stack.push(current);
				current = current.left;
			} else {
				Node<Integer> peekNode = stack.peek();
				if (peekNode.right != null && peekNode.right != lastVisited) {
					current = peekNode.right;
				} else {
					if (sb.length() == 0) {
						sb.append(peekNode);
					} else {
						sb.append(" " + peekNode);
					}
					lastVisited = stack.pop();
				}
			}			
		}
		System.out.println(sb.toString());
	}

	private void addNode(Node<Integer> rootNode, Node<Integer> newNode, int[] in) {
		int pos1 = findPos(in, newNode.value);
		Stack<Node<Integer>> stack = new Stack<Node<Integer>>();
		stack.push(rootNode);
		while (!stack.isEmpty()) {			
			Node<Integer> current = stack.pop();
			int pos2 = findPos(in, current.value);
			if (pos1 < pos2) {
				if (current.left == null) {
					current.left = newNode;
					return;
				} else {
					stack.push(current.left);
				}
			} else {
				if (current.right == null) {
					current.right = newNode;
					return;
				} else {
					stack.push(current.right);
				}
			}
		}
	}

	private int findPos(int[] in, int id) {
		for (int i = 0; i < in.length; i++) {
			if (in[i] == id) {
				return i;
			}
		}
		return -1;
	}
}


5
1 2 3 4 5
3 2 4 1 5 4
1 2 3 4
1 2 3 4 4
1 2 3 4
2 1 3 4 4
2 1 3 4
2 1 3 4 4
2 1 3 4
3 1 2 4 5
1 2 3 4 5
1 2 4 3 5 4
2 1 3 4
1 2 3 4 5
1 3 2 4 5
3 2 4 1 5 4
1 4 3 2
1 2 3 4 5
1 2 3 4 5
3 4 2 1 5 4
1 2 3 4
1 2 4 3 4
2 1 3 4
2 1 4 3 4
1 2 3 4
2 1 3 4 4
1 2 4 3
2 1 3 4 4
1 2 3 4
2 1 4 3
3 4 2 5 1 4 3 2 1 2 4 3 1
 4 3 1 2
 3 1 4 2
 4 5 3 2 1
 1 4 3 2
 4 2 3 5 1
 2 3 4 1
 4 3 2 5 1
 4 3 2 1
 4 3 1 2
 2 4 3 1
 2 3 4 1
 2 4 3 1
