Data Structures & AlgorithmsHardProduct-company MCQ pattern

A binary tree has inorder 9 5 1 7 2 12 8 4 3 11 and postorder 9 1 2 12 7 5 3 11 4 8. What is its preorder?

  1. A. 8 9 5 7 1 12 2 4 11 3
  2. B. 8 5 9 7 1 2 12 4 11 3
  3. C. 8 5 7 9 1 12 2 4 3 11
  4. D. 8 5 9 7 1 12 2 4 11 3

Correct answer: D. 8 5 9 7 1 12 2 4 11 3

The root is the last postorder key, 8; it splits the inorder into 9 5 1 7 2 12 and 4 3 11. On the left the root is 5 (with 9 on its left) and 7 has children 1 and 12, where 12 has the left child 2. On the right 4 has right child 11 whose left child is 3. Preorder: 8 5 9 7 1 12 2 4 11 3.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic