Data Structures & AlgorithmsVery hardGATE CS · Product-company placement

Keys 87, 36, 57, 23, 95, 88, 77 inserted in this order build a BST. How many of the 7! orderings of these same 7 keys, inserted into an empty BST, produce exactly the same tree?

  1. A. 15
  2. B. 45
  3. C. 3
  4. D. 90

Correct answer: B. 45

A node must come before every key in its subtree, but its two subtrees may interleave freely: count = C(n−1, |left|) × count(left) × count(right) = 45, which also equals n!/∏(subtree sizes).

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic