#!/usr/bin/env python

# -*- coding: utf-8 -*-



"""

input:

preorder & inorder



5

1 2 3 4 5

3 2 4 1 5



9

1 2 3 4 5 6 7 8 9

3 2 5 4 6 1 8 7 9



output:

postorder

3 4 2 5 1

"""



import sys





class TreeNode:

    def __init__(self, val):

        self.val = val

        self.left, self.right = None, None





def reconstruction(_pre_order, _in_order):

    # write your code here

    if not _pre_order:

        return None

    root = TreeNode(_pre_order[0])

    root_position = _in_order.index(_pre_order[0])

    root.left = reconstruction(_pre_order=_pre_order[1:1 + root_position], _in_order=_in_order[:root_position])

    root.right = reconstruction(_pre_order=_pre_order[root_position + 1:], _in_order=_in_order[root_position + 1:])

    post_order.append(root.val)

    return root.val





if __name__ == '__main__':

    _input = sys.stdin.readlines()

    array_length = int(_input[0])

    pre_order = list(map(int, _input[1].split()))

    in_order = list(map(int, _input[2].split()))

    # assert len(pre_order) == len(in_order) == array_length



    post_order = []

    reconstruction(_pre_order=pre_order, _in_order=in_order)

    print((*post_order))
#!/usr/bin/env python

# -*- coding: utf-8 -*-



"""

input:

preorder & inorder



5

1 2 3 4 5

3 2 4 1 5



9

1 2 3 4 5 6 7 8 9

3 2 5 4 6 1 8 7 9



output:

postorder

3 4 2 5 1

"""



import sys





# recursively defines a binary tree

# node.left or node.right may also be a binary tree

class TreeNode:

    def __init__(self, val):

        self.val = val

        self.left, self.right = None, None





def reconstruction(_pre_order, _in_order):

    # write your code here

    if not _pre_order:

        return None

    root = TreeNode(_pre_order[0])

    root_position = _in_order.index(_pre_order[0])

    root.left = reconstruction(_pre_order=_pre_order[1:1 + root_position], _in_order=_in_order[:root_position])

    root.right = reconstruction(_pre_order=_pre_order[root_position + 1:], _in_order=_in_order[root_position + 1:])

    post_order.append(root.val)

    return root.val





if __name__ == '__main__':

    _input = sys.stdin.readlines()

    array_length = int(_input[0])

    pre_order = list(map(int, _input[1].split()))

    in_order = list(map(int, _input[2].split()))

    # assert len(pre_order) == len(in_order) == array_length



    post_order = []

    reconstruction(_pre_order=pre_order, _in_order=in_order)

    print((*post_order))
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
input:
preorder & inorder
5
1 2 3 4 5
3 2 4 1 5
9
1 2 3 4 5 6 7 8 9
3 2 5 4 6 1 8 7 9
output:
postorder
3 4 2 5 1
"""
import sys


class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None


def reconstruction(_pre_order, _in_order):
    # write your code here
    if not _pre_order:
        return None
    root = TreeNode(_pre_order[0])
    root_position = _in_order.index(_pre_order[0])
    root.left = reconstruction(
        _pre_order=_pre_order[1 : 1 + root_position],
        _in_order=_in_order[:root_position],
    )
    root.right = reconstruction(
        _pre_order=_pre_order[root_position + 1 :],
        _in_order=_in_order[root_position + 1 :],
    )
    post_order.append(root.val)
    return root.val


if __name__ == "__main__":
    _input = sys.stdin.readlines()
    array_length = int(_input[0])
    pre_order = list(map(int, _input[1].split()))
    in_order = list(map(int, _input[2].split()))
    # assert len(pre_order) == len(in_order) == array_length
    post_order = []
    reconstruction(_pre_order=pre_order, _in_order=in_order)
    print((*post_order))

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
input:
preorder & inorder
5
1 2 3 4 5
3 2 4 1 5
9
1 2 3 4 5 6 7 8 9
3 2 5 4 6 1 8 7 9
output:
postorder
3 4 2 5 1
"""
import sys

# recursively defines a binary tree
# node.left or node.right may also be a binary tree
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None


def reconstruction(_pre_order, _in_order):
    # write your code here
    if not _pre_order:
        return None
    root = TreeNode(_pre_order[0])
    root_position = _in_order.index(_pre_order[0])
    root.left = reconstruction(
        _pre_order=_pre_order[1 : 1 + root_position],
        _in_order=_in_order[:root_position],
    )
    root.right = reconstruction(
        _pre_order=_pre_order[root_position + 1 :],
        _in_order=_in_order[root_position + 1 :],
    )
    post_order.append(root.val)
    return root.val


if __name__ == "__main__":
    _input = sys.stdin.readlines()
    array_length = int(_input[0])
    pre_order = list(map(int, _input[1].split()))
    in_order = list(map(int, _input[2].split()))
    # assert len(pre_order) == len(in_order) == array_length
    post_order = []
    reconstruction(_pre_order=pre_order, _in_order=in_order)
    print((*post_order))

- +# recursively defines a binary tree +# node.left or node.right may also be a binary tree