#include<iostream>

struct binary_tree{
  int parent;
  int left,right;
  int pre_num,in_num;
  static void preorder(binary_tree [],int);
  static void inorder(binary_tree [],int);
  static void llpostorder(binary_tree [],int);
  static void postorder(binary_tree [],int,int [],int *);
  static void w_info(int *,binary_tree [],int,int,int,int [],int []);
};

void binary_tree::preorder(binary_tree T[],int root){
  std::cout<<" "<<root;
  int left=T[root].left;
  int right=T[root].right;
  if(left==-1&&right==-1){
  }else if(right==-1){
    preorder(T,left);
  }else if(left==-1){
    preorder(T,right);
  }else{
    preorder(T,left);
    preorder(T,right);
  }
}

void binary_tree::inorder(binary_tree T[],int root){
  int left=T[root].left;
  int right=T[root].right;
  if(left==-1&&right==-1){
    std::cout<<" "<<root;
    }else if(right==-1){
    inorder(T,left);
    std::cout<<" "<<root;
  }else if(left==-1){
    std::cout<<" "<<root;
    inorder(T,right);
  }else{
    inorder(T,left);
    std::cout<<" "<<root;
    inorder(T,right);
  }
}

void binary_tree::llpostorder(binary_tree T[],int root){
  int left=T[root].left;
  int right=T[root].right;
  if(left==-1&&right==-1){
    std::cout<<" "<<root;
    }else if(right==-1){
    llpostorder(T,left);
    std::cout<<" "<<root;
  }else if(left==-1){
    llpostorder(T,right);
    std::cout<<" "<<root;
  }else{
    llpostorder(T,left);
    llpostorder(T,right);
    std::cout<<" "<<root;
  }
}

void binary_tree::postorder(binary_tree T[],int root,int R[],int *count){

  int left=T[root].left;
  int right=T[root].right;
  if(left==-1&&right==-1){
    R[*count]=root;
    *count=*count+1;
    //std::cout<<" "<<*count;
    }else if(right==-1){
    postorder(T,left,R,count);
    //std::cout<<" "<<*count;
    R[*count]=root;
    *count=*count+1;;
  }else if(left==-1){
    postorder(T,right,R,count);
    //std::cout<<" "<<*count;
    R[*count]=root;
    *count=*count+1;;
  }else{
    postorder(T,left,R,count);
    postorder(T,right,R,count);
    //std::cout<<" "<<*count;
    R[*count]=root;
    *count=*count+1;;
  }
}

void binary_tree::w_info(int* count,binary_tree T[],int start_point,int last_point,int root,int pre[],int in[]){
  *count=*count+1;
  int pre_num=T[root].pre_num;
  int in_num=T[root].in_num;
  if(start_point==last_point){
    T[root].left=-1;
    T[root].right=-1;
  }else if(in_num==start_point){
    T[root].left=-1;
    int right=pre[*count];
    T[root].right=right;
    T[right].parent=root;
    w_info(count,T,in_num+1,last_point,right,pre,in);
  }else if(in_num==last_point){
    T[root].right=-1;
    int left=pre[pre_num+1];
    T[root].left=left;
    T[left].parent=root;
    w_info(count,T,start_point,in_num-1,left,pre,in);
  }else{
    int left=pre[pre_num+1];
    T[root].left=left;
    T[left].parent=root;
    w_info(count,T,start_point,in_num-1,left,pre,in);
    int right=pre[*count];
    T[root].right=right;
    T[right].parent=root;
    w_info(count,T,in_num+1,last_point,right,pre,in);
  }
}

int main(){
  int n;
  std::cin>>n;
  binary_tree T[n+1];
  int pre[n],in[n];
  for(int i=0;i<n;i++){
    std::cin>>pre[i];
    T[pre[i]].pre_num=i;
  }
  for(int i=0;i<n;i++){
    std::cin>>in[i];
    T[in[i]].in_num=i;
  }
  int root=pre[0];
  T[root].parent=-1;
  int a=0;
  int *cc=&a;
  binary_tree::w_info(cc,T,0,n-1,root,pre,in);
  int R[n],c=0;
  int *count=&c;
  binary_tree::postorder(T,root,R,count);
  //binary_tree::llpostorder(T,root);
  for(int i=0;i<n;i++){
    std::cout<<R[i];
    if(i==n-1){
      std::cout<<std::endl;
    }else{
      std::cout<<" ";
    }
  }
  return 0;
}



Measured memory_bytes: 3096