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: WA: Presentation Error)

import java.util.*;
import java.util.Arrays;

public class Main {
    class Node {
        int id;
        int left = -1, right = -1;
        String nodeType = null;
        Node(int id, int left, int right) {
            this.id = id;
            this.left = left;
            this.right = right;
        }
        void print() {
            System.out.println(id + ", " + left + ", " + right);
        }
    }
    void postorder(Node[] nodes, int x) {
        if (nodes[x].left != -1)
            postorder(nodes, nodes[x].left);
        if (nodes[x].right != -1)
            postorder(nodes, nodes[x].right);
        System.out.print(x + " ");
    }

    Node[] nodes;
    int[] pre;
    int[] in;
    int ap;

int search(int b1p, int b2p) {
        int center = -1;
        if (ap >= pre.length) {
            return -1;
        }
        int top = pre[ap];
        if (b1p + 1 == b2p) {
            nodes[top] = new Node(top, -1, -1);
            return top;
        } else if (b1p >= b2p) {
            return -1;
        }
        for (int i = b1p; i < b2p; i++) {
            if (top == in[i]) {
                center = i;
                break;
            }
        }
        ap++;
        int left = search(b1p, center);
        if (left != -1) ap++;
        int right = search(center+1, b2p);
        nodes[top] = new Node(top, left, right);
        return top;
    }

void run() {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        nodes = new Node[n+1];
        pre = new int[n];
        in  = new int[n];
        int root = 0;
        for (int i = 0; i < n; i++) {
            int id = sc.nextInt();
            if (i == 0) root = id;
            pre[i] = id;
        }
        for (int i = 0; i < n; i++) {
            int id = sc.nextInt();
            in[i] = id;
        }
        search(0, nodes.length);
        /*
        for (int i = 1; i <= n; i++) {
            nodes[i].print();
        }*/
        postorder(nodes, root);
        System.out.println();
    }
    public static void main(String[] args) {
        new Main().run();
    }
}