#K93532. Reconstruct Binary Tree and Level-Order Traversal

    ID: 38440 Type: Default 1000ms 256MiB

Reconstruct Binary Tree and Level-Order Traversal

Reconstruct Binary Tree and Level-Order Traversal

You are given two sequences representing the inorder and postorder traversals of a binary tree. Your task is to reconstruct the binary tree and output its level-order traversal.

Recall that in the inorder traversal, the nodes are visited in the order \(\text{left subtree}\), \(\text{root}\), \(\text{right subtree}\), and in the postorder traversal, the nodes are visited in the order \(\text{left subtree}\), \(\text{right subtree}\), \(\text{root}\). Given these properties, the last element in the postorder sequence is the root of the tree. Let the position of this root in the inorder sequence be \(k\). Then, all elements before position \(k\) in the inorder sequence form the left subtree, and all elements after form the right subtree.

Your output should list the nodes as they appear in a level-order (breadth-first) traversal.

inputFormat

The input consists of two lines:

  • The first line contains space-separated integers representing the inorder traversal of the tree.
  • The second line contains space-separated integers representing the postorder traversal of the tree.

outputFormat

Output a single line of space-separated integers which represents the level-order traversal of the reconstructed binary tree.

## sample
9 3 15 20 7
9 15 7 20 3
3 9 20 15 7