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: Wrong Answer)

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

namespace _20170410_AOJ_ALDS1_7D
{
    class ALDS1_7D
    {
        static void Main(string[] args)
        {
            for (;;)
            {
                string line = Console.ReadLine();
                if (string.IsNullOrEmpty(line))
                {
                    break;
                }

                int n = int.Parse(line);

                TreeNode[] arr = new TreeNode[n + 1];
                TreeNode parent = null;
                int[] ns = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();

                for (int i = 0; i < n; i++)
                {
                    TreeNode tn = new TreeNode();
                    tn.Id = ns[i];
                    arr[tn.Id] = tn;
                    if (parent != null)
                    {
                        parent.Left = tn;
                        tn.Parent = parent;
                    }
                    parent = tn;
                }

                ns = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
                TreeNode restRoot = arr[1].GetRoot();
                for (int i = 0; i < n; i++)
                {
                    if(restRoot==null)
                    {
                        break;
                    }

                    TreeNode tn = arr[ns[i]];
                    if (restRoot == tn)
                    {
                        restRoot = tn.Left;
                        tn.Left = null;
                        tn.Right = restRoot;
                    }
                    else if (restRoot.Contains(tn))
                    {
                        restRoot = tn.Left;
                        if (restRoot != null)
                        {
                            tn.Left = null;
                            restRoot.Parent = null;
                        }
                    }
                    else
                    {
                        if(restRoot.Parent!=null)
                        {
                            restRoot.Parent.Right = null;
                        }
                        restRoot.Parent = tn;
                        tn.Right = restRoot;
                    }
                }

                TreeNode root = arr[1].GetRoot();
                WriteOrder(root);
            }
        }

        static void WriteOrder(TreeNode tn)
        {
            List<TreeNode> list = new List<TreeNode>();
            PostOrder(list, tn);
            Console.WriteLine(string.Join(" ", list.Select(x => x.Id.ToString()).ToArray()));
        }

        static void PostOrder(List<TreeNode> list, TreeNode tn)
        {
            if (tn.Left != null)
            {
                PostOrder(list, tn.Left);
            }

            if (tn.Right != null)
            {
                PostOrder(list, tn.Right);
            }

            list.Add(tn);
        }
    }

    class TreeNode
    {
        public int Id { get; set; }
        public TreeNode Parent { get; set; }
        public TreeNode Left { get; set; }
        public TreeNode Right { get; set; }
        public int Depth { get; internal set; }
        public int Height { get; internal set; }
        public TreeNode()
        {
        }

        public TreeNode GetRoot()
        {
            TreeNode temp = this;
            while (temp.Parent != null)
            {
                temp = temp.Parent;
            }

            return temp;
        }

        public bool Contains(TreeNode tn)
        {
            bool rv = false;
            rv = (
                this == tn ||
                (Left != null && Left.Contains(tn)) ||
                (Right != null && Right.Contains(tn)));
            return rv;
        }
    }
}