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: Accepted)

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.PreOrderDic.Add(this.PreOrder[i], i);
             this.InOrder.Add(int.Parse(instr[i]));
        }

        TreeNd ansData = this.SetTree(this.InOrder);
        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 Dictionary<int, int> PreOrderDic = new Dictionary<int, 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 int GetRootTreeId(List<int> list)
    {
        int minId = -1;
        int minIdx = int.MaxValue;
        foreach (int num in list)
        {
            if (this.PreOrderDic[num] < minIdx)
            {
                minIdx = this.PreOrderDic[num];
                minId = num;
            }
        }
        return minId;
    }

    private TreeNd SetTree(List<int> list){
        if (list == null || list.Count <= 0)
        {
            return null;
        }
        if (list.Count == 1)
        {
            return new TreeNd(list[0]);
        }
        int rootId = this.GetRootTreeId(list);
        int rootIndex = list.IndexOf(rootId);
        TreeNd rootNode = new TreeNd(rootId);

        List<int> left = new List<int>();
        List<int> right = new List<int>();
        for (int i = 0; i < rootIndex; i++)
        {
            left.Add(list[i]);
        }
        for (int i = rootIndex + 1; i < list.Count; i++)
        {
            right.Add(list[i]);
        }
        rootNode.LeftChild = this.SetTree(left);
        rootNode.RightChild = this.SetTree(right);
        if (rootNode.LeftChild != null)
        {
            rootNode.LeftChild.Parent = rootNode;
        }
        if (rootNode.RightChild != null)
        {
            rootNode.RightChild.Parent = rootNode;
        }
        return rootNode;
    }

    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();
    }
}