Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Reconstruction of a Tree

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.

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.

Constraints

$1 \leq n \leq 40$

Sample Input 1

5
1 2 3 4 5
3 2 4 1 5

Sample Output 1

3 4 2 5 1

Sample Input 2

4
1 2 3 4
1 2 3 4

Sample Output 2

4 3 2 1

Solution in JAVA (judge verdict: Accepted)

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 {
		int id, pos;
		Node left, right;
		public Node(int id) {
			this.id = id;
			pos = -1;
			left = right = null;
		}
	}
	
	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 = -1;
		int rootPos = -1;
		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];
			}
			if (rootId == in[i]) {
				rootPos = i;
			}
		}
		Node rootNode = new Node(pre[0]);
		rootNode.pos = rootPos;
		for (int i = 1; i < n; i++) {
			addNode(rootNode, new Node(pre[i]), in);
		}
		System.out.println(postOrder(rootNode));
	}

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

	private void addNode(Node rootNode, Node newNode, int[] in) {
		newNode.pos = findPos(newNode.id, in);
		Stack<Node> stack = new Stack<Node>();
		stack.push(rootNode);
		while (!stack.isEmpty()) {
			Node current = stack.pop();
			if (current.pos == -1) {
				current.pos = findPos(current.id, in);
			}
			if (newNode.pos < current.pos) {
				if (current.left == null) {
					current.left = newNode;
				} else {
					stack.push(current.left);
				}
			} else {
				if (current.right == null) {
					current.right = newNode;
				} else {
					stack.push(current.right);
				}
			}
		}
	}

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