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 Python3 (judge verdict: Accepted)

from typing import List


def reconstruct_trees(preordered_list: List[int],
                      inordered_list: List[int], node: int) -> None:
    global trees
    in_node_index = inordered_list.index(node)
    in_left_list = inordered_list[:in_node_index]
    in_right_list = inordered_list[in_node_index + 1:]
    _ = preordered_list.index(node)
    pr_left_list = preordered_list[1: 1 + len(in_left_list)]
    pr_right_list = preordered_list[1 + len(in_left_list):]

    if 0 == len(in_left_list):
        pass
    else:
        next_node = pr_left_list[0]
        trees[next_node]['parent'] = node
        trees[node]['left-child'] = next_node
        if 1 < len(in_left_list):
            reconstruct_trees(pr_left_list, in_left_list, next_node)
    if 0 == len(in_right_list):
        pass
    else:
        next_node = pr_right_list[0]
        trees[next_node]['parent'] = node
        trees[node]['right-child'] = next_node
        if 1 < len(in_right_list):
            reconstruct_trees(pr_right_list, in_right_list, next_node)


def create_postorder(node: int) -> None:
    global trees
    global postordered_list
    left_child = trees[node]['left-child']
    right_child = trees[node]['right-child']
    if -1 != left_child:
        create_postorder(left_child)
    if -1 != right_child:
        create_postorder(right_child)
    postordered_list.append(node)


if __name__ == "__main__":
    node_num = int(input())
    trees = {idx + 1:
             {"parent": -1, "left-child": -1, "right-child": -1}
             for idx in range(node_num)}
    preordered_list = list(map(lambda x: int(x), input().split()))
    inordered_list = list(map(lambda x: int(x), input().split()))
    postordered_list = []  # type: ignore

    root_node = preordered_list[0]

    reconstruct_trees(preordered_list, inordered_list, root_node)
    create_postorder(root_node)

    print(" ".join([str(elem) for elem in postordered_list]))