GATE / CS ARCHIVE

GATE CSE 1997 / QUESTION 4.5

Valid BST preorder

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

GATE 1997, Q4.5 2 marks · MCQ
Report issue

Binary Search & Balanced Trees

Question 4.5, GATE 1997: A BST contains 1…8. Which preorder is valid:53124786, 53126487, 53241678, 53124768?
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

For D, root 5 divides the remaining sequence into left subtree 3, 1, 2, 4 and right subtree 7, 6, 8. In the left subtree, 3 has left subtree 1→right child 2 and right child 4. In the right subtree, 7 has children 6 and 8. This constructs the required BST. A gives 7, 8, 6 after entering root 7’s right subtree before returning to a smaller value 6. B returns to 4 after beginning root 5’s right subtree at 6. C returns to 1 after entering the right subtree of 3 at 4. These violate preorder’s contiguous subtree blocks, leaving D.

Original paper, page 4