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 Ruby (judge verdict: Runtime Error)

class BinTree
  attr_accessor :node
  def initialize(n)
    @node = Array.new(n){Node.new()}
  end
  def preorder(n=root())
    str = ""
    return str if n < 0
    #root
    str << sprintf(" %s", n)
    #left
    str << preorder(node[n].left)
    #right
    str << preorder(node[n].right)
    return str
  end
  def inorder(n=root())
    str = ""
    return str if n < 0
    #left
    str << inorder(node[n].left)
    #root
    str << sprintf(" %s", n)
    #right
    str << inorder(node[n].right)
    return str
  end
  def postorder(n=root())
    arr = []
    return [] if n < 0
    #left
    arr += postorder(node[n].left)
    #right
    arr += postorder(node[n].right)
    arr += [n]
  end
  def root
    #find root
    r = 0
    node.size.times {|i|
      if node[i].parent == -1 then
        r = i
        break
      end
    }
    return r
  end
  def add(i, l, r)
    node[i].left = l
    node[i].right = r
    node[l].parent = i unless l == -1
    node[r].parent = i unless r == -1
  end
  def order_to_tree(preorder, inorder)
    n = preorder.size
    i = preorder[0]
    idx_root = inorder.index(i)
    if idx_root == 0 then
      inorder_left = []
    else
      inorder_left = inorder[0..idx_root-1]
    end
    inorder_right = inorder[idx_root+1..n-1]
    idx = preorder.index(inorder_right[0])
    preorder_left = preorder[1..idx-1]
    preorder_right = preorder[idx..n-1]
#    p inorder_right.size, preorder_right.size

    if inorder_left.size == 1 then
      node[i].left = inorder_left[0]
      node[inorder_left[0]].parent = i
    end
    if inorder_right.size == 1 then
      node[i].right = inorder_right[0]
      node[inorder_right[0]].parent = i
    end
    if inorder_left.size > 1 then
      node[i].left = self.order_to_tree(preorder_left, inorder_left)
      node[node[i].left].parent = i
    end    
    if inorder_right.size > 1 then
      node[i].right = self.order_to_tree(preorder_right, inorder_right)
      node[node[i].right].parent = i
    end
    return i
  end
end

class Node
  attr_accessor :parent, :left, :right
  def initialize
    @parent = -1
    @left = -1
    @right = -1
  end
end

n = gets.to_i
tree = BinTree.new(n)

preorder = gets.split.map(&:to_i).map{|i| i -= 1}
inorder = gets.split.map(&:to_i).map{|i| i -= 1}

tree.order_to_tree(preorder, inorder)

puts tree.postorder().map{|i| i += 1}.join(" ")