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

import scala.io.StdIn.{readInt, readLine}
object Main {
    def main(args: Array[String]): Unit = {
        val n = readInt;
        val pre = readLine.split(" ").map(_.toInt);
        val in = readLine.split(" ").map(_.toInt);
        val tree = Tree();
        tree.build(n, pre, in);
        println(tree.postorder().mkString(" "));
    }
}

case class Tree() {
    case class TreeNode(value: Int, left: TreeNode, right: TreeNode);
    var root: TreeNode = null;
    
    def preorder(): List[Int] = {
        def _preorder(node: TreeNode, ret: List[Int]): List[Int] = {
            if (node == null) {
                ret;
            } else {
                val ret1 = node.value :: ret;
                val ret2 = _preorder(node.left, ret1);
                val ret3 = _preorder(node.right, ret2);
                ret3;
            }
        }
        _preorder(root, Nil).reverse;
    }
    
    def postorder(): List[Int] = {
        def _preorder(node: TreeNode, ret: List[Int]): List[Int] = {
            if (node == null) {
                ret;
            } else {
                val ret1 = _preorder(node.left, ret);
                val ret2 = _preorder(node.right, ret1);
                val ret3 = node.value :: ret2;
                ret3;
            }
        }
        _preorder(root, Nil).reverse;
    }
    
    def build(size: Int, pre: Array[Int], in: Array[Int]): Unit = {
        // pre の index を示すポインタ
        //  pre(prePtr) を value にもつノードが生成されたときにインクリメントする。
        var prePtr = 0
        def buildSubTree(lip: Int, rip: Int): TreeNode = {
            if (lip > rip) {
                // 探索するノードが存在しない場合はnullを返す
                null;
            } else if (lip == rip) {
                //  探索するノードが1つしかない場合は、子ノードが null の部分木を返す
                val value: Int = pre(prePtr);
                prePtr += 1;
                TreeNode(value, null, null);
            } else {
                val value: Int = pre(prePtr);
                val pivot: Int = (lip to rip).find(i => in(i) == value).get; // 無いわけない
                prePtr += 1;
                //println(s"[${value}]  prePtr : ${prePtr}");
                //if (lip == pivot) println(s"[${value}]  left: null") else println(s"[${value}]  left: " + (lip to pivot - 1).map(in(_)).mkString(" "));
                val leftSubTree = buildSubTree(lip, pivot - 1);
                //if (rip == pivot) println(s"[${value}]  right: null") else println(s"[${value}]  right: " + (pivot + 1 to rip).map(in(_)).mkString(" "));
                val rightSubTree = buildSubTree(pivot + 1,  rip);
                TreeNode(value, leftSubTree, rightSubTree);
            }
        }
        root = if (size < 1) null else {buildSubTree(0, size - 1)};
    }
}