Data Structures & AlgorithmsModerateCognizant · Capgemini pattern

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

  1. A. 8 4 5 2 6 7 3 1
  2. B. 8 4 2 5 6 7 3 1
  3. C. 4 8 5 2 6 7 3 1
  4. D. 8 4 5 2 7 6 3 1

Correct answer: A. 8 4 5 2 6 7 3 1

Root 1 splits the inorder into 4 8 2 5 and 6 3 7. In the left part the root is 2; then 4 has 8 as its RIGHT child (8 comes after 4 in inorder). So postorder is 8 4 5 2, 6 7 3, 1. Making 8 a left child gives 4 8 … wrongly.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic