GATE / CS ARCHIVE

GATE CSE 2020 / QUESTION 5

GATE CSE 2020 Q5

Data Structures · Binary Search & Balanced Trees · Full GATE CSE 2020 paper

GATE 2020, Q5 1 mark · MCQ
Report issue

Binary Search & Balanced Trees

Question 5, GATE 2020: A BST preorder is 15,10,12,11,20,18,16,19. Find postorder. A 10,11,12,15,16,18,19,20; B 11,12,10,16,19,18,20,15; C 20,19,18,16,15,12,11,10; D 19,16,18,20,11,12,10,15.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Step through it (19 steps)

Worked explanation

Preorder lists root, left subtree, right subtree; the BST ordering tells us where the left subtree ends. The reconstructed tree has edges 15→10(left),15→20(right),10→12(right),12→11(left),20→18(left),18→16(left),18→19(right). Postorder recursively visits left, right, root, hence the entire left subtree appears before the right subtree and 15 must be last. Option A is sorted inorder, C follows a different ordering, and D puts the right subtree before the left. This explicit edge list also makes the reconstruction checkable without a picture.

Derivation and answer

Root15 splits the preorder into left10, 12, 11 and right20, 18, 16, 19. On the left, 10 has right child12 with left child11, giving postorder 11, 12, 10. On the right, 20 has left child18 with children 16, 19, giving 16, 19, 18, 20. Append the root: 11, 12, 10, 16, 19, 18, 20, 15, B.

Original paper, page 5 · Answer key