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

namespace ALDS1_7_D
{
    public class Program

    {
        public static void Main(string[] args)
        {
            int n = ReadInt();
            int[] preOrder = ReadIntAr();
            int[] inOrder = ReadIntAr();

            BinaryTreeNodeList<int> nodes = new BinaryTreeNodeList<int>(n);
            BinaryTree<int> bt = new BinaryTree<int>();

            for (int i = 0 ; i < n ; i++) { nodes[i] = new BinaryTreeNode<int>(inOrder[i]); }

            int index = 0;
            bt.Root = Reconstruct(nodes, preOrder, inOrder, ref index);
            List<int> res = new List<int>();
            bt.PostorderTraversal(bt.Root, res);

            Console.WriteLine(String.Join(" ", res.Select(x => x.ToString()).ToArray()));
        }

        static BinaryTreeNode<int> Reconstruct(BinaryTreeNodeList<int> nodes, int[] preoder, int[] inorder, ref int i)
        {
            var root = preoder[i];
            var index = Array.IndexOf(inorder, root);
            

            if (index == 0)
                nodes.FindByValue(root).Left = null;
            else
            {
                i++;
                BinaryTreeNode<int> left = Reconstruct(nodes, preoder, inorder.Take(index).ToArray(), ref i);
                nodes.FindByValue(root).Left = left;
            }

            if (index == inorder.Length - 1)
                nodes.FindByValue(root).Right = null;
            else
            {
                i++;
                BinaryTreeNode<int> right = Reconstruct(nodes, preoder, inorder.Skip(index + 1).Take(inorder.Length - index - 1).ToArray(), ref i);
                nodes.FindByValue(root).Right = right;
            }
            return nodes.FindByValue(root);
        }


        static string ReadSt() { return Console.ReadLine(); }
        static int ReadInt() { return int.Parse(Console.ReadLine()); }
        static long ReadLong() { return long.Parse(Console.ReadLine()); }
        static double ReadDouble() { return double.Parse(Console.ReadLine()); }
        static string[] ReadStAr(char sep = ' ') { return Console.ReadLine().Split(sep); }
        static int[] ReadIntAr(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Split(sep), e => int.Parse(e)); }
        static long[] ReadLongAr(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Split(sep), e => long.Parse(e)); }
        static double[] ReadDoubleAr(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Split(sep), e => double.Parse(e)); }

    }
    
    public class BinaryTreeNode<T> : Node<T>
    {
        public BinaryTreeNode() : base() { }
        public BinaryTreeNode(T data) : base(data, null) { }
        public BinaryTreeNode(T data, BinaryTreeNode<T> left, BinaryTreeNode<T> right)
        {
            Value = data;
            Neighbors = new NodeList<T> { left, right };
        }

        public BinaryTreeNode<T> Left
        {
            get
            {
                if (base.Neighbors == null) return null;
                else return (BinaryTreeNode<T>)Neighbors[0];
            }
            set
            {
                if (base.Neighbors == null) base.Neighbors = new NodeList<T>(2);
                base.Neighbors[0] = value;
                if ( value != null) value.Parent = Neighbors[0];
            }
        }

        public BinaryTreeNode<T> Right
        {
            get
            {
                if (base.Neighbors == null) return null;
                else return (BinaryTreeNode<T>)base.Neighbors[1];
            }
            set
            {
                if (base.Neighbors == null) base.Neighbors = new NodeList<T>(2);
                base.Neighbors[1] = value;
                if (value != null)  value.Parent = Neighbors[1];
            }
        }
    }

    public class BinaryTreeNodeList<T> :NodeList<T>
    {
        public BinaryTreeNodeList() : base() { }
        public BinaryTreeNodeList(int initialSize)
        { for (int i = 0 ; i < initialSize ; i++) Add(default(BinaryTreeNode<T>)); }

        public new BinaryTreeNode<T> FindByValue(T value)
        {
            foreach (BinaryTreeNode<T> node in this) if (node.Value.Equals(value)) return node;
            return null;
        }
    }

    public class BinaryTree<T>
    {

        public BinaryTreeNode<T> Root { get; set; }

        public BinaryTree() { Root = null; }
        public virtual void Clear() { Root = null; }

        public void PreorderTraversal(BinaryTreeNode<T> current,List<T> res)
        {
            if (current != null)
            {
                res.Add(current.Value);
                PreorderTraversal(current.Left, res);
                PreorderTraversal(current.Right,res);
            }
        }

        public void InorderTraversal(BinaryTreeNode<T> current, List<T> res)
        {
            if (current != null)
            {
                InorderTraversal(current.Left, res);
                res.Add(current.Value);
                InorderTraversal(current.Right, res);
            }
        }

        public void PostorderTraversal(BinaryTreeNode<T> current, List<T> res)
        {
            if (current != null)
            {
                PostorderTraversal(current.Left, res);
                PostorderTraversal(current.Right, res);
                res.Add(current.Value);
            }
        }

    }

 public class Node<T>
    {
        public T Value { get; set; }
        public Node<T> Parent { get; set; }
        protected NodeList<T> Neighbors { get; set; }

        public Node() { }
        public Node(T data) : this(data, null) { }
        public Node(T data, NodeList<T> neighbors)
        {
            Value = data;
            Neighbors = neighbors;
        }
    }

    public class NodeList<T> : List<Node<T>>
    {
        public NodeList() : base() { }
        public NodeList(int initialSize)
        { for (int i = 0 ; i < initialSize ; i++) Add(default(Node<T>)); }

        public Node<T> FindByValue(T value)
        {
            foreach (Node<T> node in this) if (node.Value.Equals(value)) return node;
            return null;
        }
    }

}



Measured memory_bytes: 20432