# Tree - Reconstruction of a Tree
class Node:
    none = 0
    right = 1
    left = -1
    def is_leaf(self):
        d = 1 if self.right else 0
        d += 1 if self.left else 0
        return self.depth > 0 and d == 0
    def num_child(self):
        d = 1 if self.right else 0
        d += 1 if self.left else 0
        return d
    
    def __init__(self, id):
        self.id = id
        self.parent = None
        self.sibling = None
        # ?????¨?????¨????????¨?????¨????????¨
        self.right,self.left = None,None
        self.depth = 0
        self.height = 0
        self.position = Node.none
    def __str__(self):
        p = self.parent.id if self.parent else -1
        s = self.sibling.id if self.sibling else -1
        d = 1 if self.right else 0
        d += 1 if self.left else 0
        typ = 'root'
        if self.depth > 0: typ = 'internal node'
        if d == 0 and self.depth > 0: typ = 'leaf'
        h = self.height
        return 'node {0}: parent = {1}, sibling = {2}, degree = {3}, depth = {4}, height = {5}, {6}'.format(self.id, p, s, d, self.depth, h, typ)

def calc_cld_depth(nd):
    """?????????????????±???????¨??????????"""
    if nd.left:
        nd.left.depth = nd.depth + 1
        calc_cld_depth(nd.left)
    if nd.right:
        nd.right.depth = nd.depth + 1
        calc_cld_depth(nd.right)

def rec(preo, ino):
    idx = -1
    proot = None # ??¨?????¨????????????
    for p in preo:
        if p in ino:
            idx = ino.index(p)
            proot = p
            break
    else:
        raise # unexpected error
    # ??????idx???????????\??£??????
    pleft = ino[:idx]
    pright = ino[idx+1:]
    prlen = len(pright)
    pllen = len(pleft)
    if prlen == 1:
        # ????????????pright??????????????????proot??????
        pright[0].parent = proot
        pright[0].depth = proot.depth + 1
        proot.right = pright[0]
    if pllen == 1:
        # ????????????pleft??????????????????proot??????
        pleft[0].parent = proot
        pleft[0].depth = proot.depth + 1
        proot.left = pleft[0]
    # ??°??????ino???????????????????????§????????????preo???????????£?????????
    if prlen > 1:
        for r in pright:
            r.parent = proot
            r.depth = proot.depth + 1
        pprt = rec(preo[1+pllen:], pright)
        proot.right = pprt
    if pllen > 1:
        for l in pleft:
            l.parent = proot
            l.depth = proot.depth + 1
        pprt = rec(preo[1:1+pllen], pleft)
        proot.left = pprt

    if prlen == 0: proot.right = None
    if pllen == 0: proot.left = None
    return proot

n = int(input())
nds = [Node(i+1) for i in range(n)] # id??????????????§??????(????????????)????????????id???1
preo = [nds[int(i)-1] for i in input().split()]
ino = [nds[int(i)-1] for i in input().split()]
# ????????°??¨??????????????\????????????????????°???????????±???????????????
root = rec(preo, ino)
from collections import deque
dq = deque()
# Postorder
visited = [None]*n
cnt = 0
dq.append(root)
while len(dq) > 0:
    pp = dq.pop()
    dl = pp.num_child()
    if dl == 0:
        visited[cnt] = pp
        cnt += 1
        continue
    if dl > 0:
        l,r = pp.left, pp.right
        if (l and l not in visited) or (r and r not in visited):
            if pp: dq.append(pp)
            if r: dq.append(r)
            if l: dq.append(l)
        else:
            visited[cnt] = pp
            cnt += 1
            continue

ss = ''
for v in visited: ss += ' '+str(v.id)
print(ss[1:])


Measured memory_bytes: 8088