GATE Computer ScienceVery hardGATE CS (PYQ pattern)

Among all 429 structurally different binary search trees on the keys 1 to 7, in how many is key 3 a leaf?

  1. A. 143
  2. B. 132
  3. C. 66
  4. D. 90

Correct answer: B. 132

Two keys adjacent in sorted order are always in an ancestor-descendant relation in a BST (no key lies between them to split them apart). Key 3 is a leaf exactly when 2 and 4 are both its ancestors, and deleting that leaf is a bijection onto all BSTs on the six keys {1, 2, 4, 5, 6, 7}: in each of those, 2 and 4 are adjacent and hence related, and the empty slot between them is exactly where 3 must hang. So the count is C6 = 132 (a brute force over the 429 trees agrees).

More Programming and Data Structures (GATE) questions

Exams that ask this topic