GATE / CS ARCHIVE

GATE CSE 2017 SET 2 / QUESTION 36

GATE CSE 2017 Set 2 Q36

Data Structures · Binary Search & Balanced Trees · Full GATE CSE 2017 Set 2 paper

GATE 2017 Set 2, Q36 2 marks · MCQ
Report issue

Binary Search & Balanced Trees

Question 36, GATE 2017: A binary search tree has preorder 12,8,6,2,7,9,10,16,15,19,17,20. Which is its postorder? A:2,6,7,8,9,10,12,15,16,17,19,20. B:2,7,6,10,9,8,15,17,20,19,16,12. C:7,2,6,8,9,10,20,17,19,15,16,12. D:7,6,2,10,9,8,15,16,17,20,19,12.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Step through it (27 steps)

1. Preorder visits root,left,right, so the first value 12 is the root. The contiguous values below 12 form its left preorder 8,6,2,7,9,10; the remaining values form right preorder 16,15,19,17,20.

2. Reconstruct the left side: root 8 has left root 6 with children 2 and 7, and right root 9 with right child 10. Its left-right-root postorder is 2,7,6,10,9,8.

3. On the right, root 16 has left child 15 and right root 19 with children 17 and 20. Its postorder is 15,17,20,19,16. Concatenate left postorder, right postorder and final root 12, obtaining B. Sorting the values would instead produce inorder, not postorder.

Original paper, page 14 · Answer key