14874 - Build binary tree from inorder and postorder   

Description

You are given two sequences representing the inorder traversal and postorder traversal of a binary tree consisting of n unique nodes.

Your task is to reconstruct the unique binary tree defined by these traversals and output its preorder traversal.

The first input line contains the inorder traversal (from left subtree → root → right subtree).

The second input line contains the postorder traversal (from left subtree → right subtree → root).

Compute and output the preorder traversal (root → left subtree → right subtree) of the constructed binary tree.

sample 1:

inorder    : 2 1 3

postorder: 2 3 1

preorder  : 1 2 3

sample 2:

inorder    : 4 2 5 1 3

postorder: 4 5 2 3 1

preorder  : 1 2 4 5 3

Input

Two lines of integers, each line consists of n unique nodes, where 1 <= n <= 100.

First line: inorder traversal

Second line: postorder traversal

Both sequences contain exactly the same set of values with no duplicates.

Output

One line of integers: preorder traversal of the constructed binary tree.

Ensure that the output, including formatting, exactly matches the provided samples.

Sample Input  Download

Sample Output  Download

Tags

11410EE 231002



Discuss