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 C# (judge verdict: WA: Presentation Error)

using System;
using System.Collections.Generic;
using System.Text;

class Program
{

    public void Proc()
    {
        int nodeCount = int.Parse(Console.ReadLine());
        string[] inpt = Console.ReadLine().Split(' ');
        List<int> preOrder = new List<int>();
        List<int> inOrder = new List<int>();
        for(int i=0; i<nodeCount; i++) {
            preOrder.Add(int.Parse(inpt[i]));
        }
        inpt = Console.ReadLine().Split(' ');
        for(int i=0; i<nodeCount; i++) {
            inOrder.Add(int.Parse(inpt[i]));
        }
        Dictionary<int, TreeNd> dic = new Dictionary<int, TreeNd>();
        List<TreeNd> list = new List<TreeNd>();
        TreeNd root = new TreeNd(preOrder[0]);
        dic.Add(root.NodeId, root);
        list.Add(root);
        for (int i = 1; i < nodeCount; i++)
        {
            int nodeId = preOrder[i];
            int prevId = preOrder[i - 1];
            TreeNd prevNd = dic[prevId];
            TreeNd newNd = null;
            if (dic.ContainsKey(nodeId))
            {
                newNd = dic[nodeId];
            }
            else
            {
                newNd = new TreeNd(nodeId);
                dic.Add(nodeId, newNd);
                list.Add(newNd);
            }
            while (prevNd != null)
            {
                if (prevNd.Parent != null && ( prevNd.Parent.SubLeft == null || prevNd.Parent.SubRight == null))
                {
                    // ????????¢????????????????????????
                    int idx = inOrder.IndexOf(nodeId);
                    int idxSib = inOrder.IndexOf(prevId);
                    int idxPare = inOrder.IndexOf(prevNd.Parent.NodeId);
                    if (idxSib < idxPare && idxPare < idx)
                    {
                        // ????????§??????
                        prevNd.Parent.SubLeft = prevNd;
                        prevNd.Parent.SubRight = newNd;
                        newNd.Parent = prevNd.Parent;
                    }
                }
                if (newNd.Parent == null)
                {
                    int idx = inOrder.IndexOf(nodeId);
                    int idxPare = inOrder.IndexOf(prevNd.NodeId);
                    if (prevNd.SubLeft == null && prevNd.SubRight == null)
                    {
                        // ?????´????????§??????????????§
                        if (idx < idxPare)
                        {
                            // ?????????
                            newNd.Parent = prevNd;
                            prevNd.SubLeft = newNd;
                        }
                    }
                    if (newNd == null && prevNd.SubRight == null)
                    {
                        // ???????????§??????????????§
                        if (idxPare < idx)
                        {
                            newNd.Parent = prevNd;
                            prevNd.SubRight = newNd;
                        }

                    }
                }
                if (newNd.Parent == null)
                {
                    prevNd = prevNd.Parent;
                }
                else
                {
                    break;
                }
            }
        }
        this.Output(root);
        Console.WriteLine(this.PostOrder.ToString());
    }

    private StringBuilder PostOrder = new StringBuilder();
    private void Output(TreeNd parent)
    {
        if (parent.SubLeft != null)
        {
            this.Output(parent.SubLeft);
        }
        if (parent.SubRight != null)
        {
            this.Output(parent.SubRight);
        }
        PostOrder.Append(" " + parent.NodeId);
    }

    public class TreeNd
    {
        public int NodeId = -1;
        public TreeNd Parent = null;

        public TreeNd(int id)
        {
            this.NodeId = id;
        }

        public TreeNd SubLeft = null;
        public TreeNd SubRight = null;

    }

    static void Main(string[] args)
    {
        Program prg = new Program();
        prg.Proc();
    }
}