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.Text;

class Program
{

    public void Proc()
    {
        int nodeCount = int.Parse(Console.ReadLine());
        this.PreOrderText = Console.ReadLine();
        this.InOrderText = Console.ReadLine();
        // testcode
        //this.PreOrderText = "1 2 3 4";
        //this.InOrderText = "1 2 3 4";
        string[] prestr = this.PreOrderText.Split(' ');
        string[] instr = this.InOrderText.Split(' ');

        this.PreOrder = new int[nodeCount];
       
        for(int i=0; i<nodeCount; i++) {
             this.PreOrder[i] = int.Parse(prestr[i]);
             this.InOrder.Add(int.Parse(instr[i]));
        }

        TreeNd root = new TreeNd(this.PreOrder[0]);
        TreeNd ansData = this.SetTree(1, root);
        string ansText = GetPostorderText(ansData);
        Console.WriteLine(ansText);
    }

    public string GetPostorderText(TreeNd dat)
    {
        if (dat == null)
        {
            return string.Empty;
        }
        StringBuilder sb = new StringBuilder();
        if (dat.LeftChild != null)
        {
            string tmp = this.GetPostorderText(dat.LeftChild);
            if (!string.IsNullOrEmpty(tmp))
            {
                sb.Append(tmp);
            }
        }
        if (dat.RightChild != null)
        {
            string tmp = this.GetPostorderText(dat.RightChild);
            if (!string.IsNullOrEmpty(tmp))
            {
                if (sb.Length > 0)
                {
                    sb.Append(" ");
                }
                sb.Append(tmp);
            }
        }
        if (sb.Length > 0)
        {
            sb.Append(" ");
        }
        sb.Append(dat.NodeId);
        return sb.ToString();
    }

    private int[] PreOrder;
    private List<int> InOrder = new List<int>();

    private String PreOrderText = string.Empty;
    private string InOrderText = string.Empty;

    private string GetInorderText(TreeNd dat)
    {
        StringBuilder sb = new StringBuilder();
        string left = string.Empty;
        if (dat.LeftChild != null)
        {
            left = this.GetInorderText(dat.LeftChild);
        }
        if (!string.IsNullOrEmpty(left))
        {
            sb.Append(left);
        }
        if (sb.Length > 0)
        {
            sb.Append(" ");
        }
        sb.Append(dat.NodeId);
        string right = string.Empty;
        if (dat.RightChild != null)
        {
            right = this.GetInorderText(dat.RightChild);
        }
        if (!string.IsNullOrEmpty(right))
        {
            sb.Append(" ");
            sb.Append(right);
        }
        return sb.ToString();
    }
    private TreeNd SetTree(int idx, TreeNd root){
        if (idx >= PreOrder.Length)
        {
            // ???????????????
            string tmpAns = this.GetInorderText(root);
            if (tmpAns.Equals(this.InOrderText))
            {
                return root;
            }
            else
            {
                return null;
            }
            
        }
        TreeNd ans = null;
        TreeNd cop = (TreeNd)root.Clone();
        TreeNd parent = cop.SearchById(this.PreOrder[idx - 1]);

        while (parent != null)
        {
            TreeNd tmp = new TreeNd(this.PreOrder[idx]);

            if (parent.LeftChild == null)
            {
                // ???????????´???(??????????????´??????
                int idx1 = this.InOrder.IndexOf(this.PreOrder[idx - 1]);
                int idx2 = this.InOrder.IndexOf(this.PreOrder[idx]);
                if (idx2 < idx1)
                {
                    tmp.Parent = parent;
                    parent.LeftChild = tmp;

                    ans = SetTree(idx + 1, cop);
                    if (ans != null)
                    {
                        return ans;
                    }
                    parent.LeftChild = null;
                }
            }
            if (parent.RightChild == null)
            {
                // ???????????´???(??????????????´??????
                int idx1 = this.InOrder.IndexOf(this.PreOrder[idx - 1]);
                int idx2 = this.InOrder.IndexOf(this.PreOrder[idx]);
                if (idx1 > idx2)
                {
                    tmp.Parent = parent;
                    parent.RightChild = tmp;

                    ans = SetTree(idx + 1, cop);
                    if (ans != null)
                    {
                        return ans;
                    }
                    parent.RightChild = null;
                }
            }
            parent = parent.Parent;
        }

        return ans;
    }

    public class TreeNd:ICloneable
    {
        public TreeNd Parent;
        public int NodeId;
        public TreeNd LeftChild;
        public TreeNd RightChild;


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

        public TreeNd SearchById(int id)
        {
            TreeNd ret = null;
            if (id == this.NodeId)
            {
                ret = this;
            }
            else if(this.LeftChild != null)
            {
                ret = this.LeftChild.SearchById(id);
            }
            if (ret == null && this.RightChild != null)
            {
                ret = this.RightChild.SearchById(id);
            }
            return ret;
        }

        public object Clone()
        {
            TreeNd tn = new TreeNd(this.NodeId);
            if (this.LeftChild != null)
            {
                tn.LeftChild = (TreeNd)this.LeftChild.Clone();
                tn.LeftChild.Parent = tn;
            }
            if (this.RightChild != null)
            {
                tn.RightChild = (TreeNd)this.RightChild.Clone();
                tn.RightChild.Parent = tn;
            }

            return tn;
        }
    }


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