14360 - 2024_DS_Summer_Lab10_Tree Reconstruct   

Description

You are given the preorder and inorder traversal of a binary tree. Your job is to reconstruct the tree and print the postorder traversal of the tree.

Note
- Each vertex of the tree is labeled 1 ~ n distinctly.

Input

Each input is given in three lines.

The first line contains an integer `n`: The number of vertices of the binary tree.

The second line is the preorder traversal of the tree.

The last line is the inorder traversal of the tree.

Note
- For both traversals, each vertex is seperated by an empty space.

Restriction
- 1 <= n <= 2*105

Output

Output should be printed in one line. Containing the postorder traversal of the tree. Each vectex of the traversal should be seperated with an empty space.

Sample Input  Download

Sample Output  Download

Tags




Discuss