The pre-order traversal of a binary search tree is given by…

2017

The pre-order traversal of a binary search tree is given by 12,8,6,2,7,9,10,16,15,19,17,20.

Then the post-order traversal of this tree is

  1. A.

    2, 6, 7, 8, 9, 10, 12, 15, 16, 17, 19, 20

  2. B.

    2, 7, 6, 10, 9, 8, 15, 17, 20, 19, 16, 12

  3. C.

    7, 2, 6, 8, 9, 10, 20, 17, 19, 15, 16, 12

  4. D.

    7, 6, 2, 10, 9, 8, 15, 16, 17, 20, 19, 12

Attempted by 235 students.

Show answer & explanation

Correct answer: B

Key insight: in a BST the preorder sequence gives root, then the entire left subtree, then the entire right subtree. Use that to split the sequence and then produce post-order (left, right, root).

  • Root: the first preorder value is 12. Everything less than 12 before the first value greater than 12 belongs to the left subtree: [8,6,2,7,9,10]. The remaining values are the right subtree preorder: [16,15,19,17,20].

  • Left subtree (root 8): split its preorder [8,6,2,7,9,10] into left [6,2,7] and right [9,10]. For subtree rooted at 6: left 2 and right 7 give post-order 2,7,6. For subtree rooted at 9 with right child 10, post-order is 10,9. Combine to get left-subtree post-order 2,7,6,10,9,8.

  • Right subtree (root 16): preorder [16,15,19,17,20] splits into left [15] and right [19,17,20]. Left gives 15. Right subtree with root 19 has left 17 and right 20, so its post-order is 17,20,19. Combine to get right-subtree post-order 15,17,20,19,16.

  • Final post-order: concatenate left-subtree post-order, right-subtree post-order, then root.

Answer: 2, 7, 6, 10, 9, 8, 15, 17, 20, 19, 16, 12

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir