Data Structures & AlgorithmsVery hardGATE CS · Product-company placement

Keys 94, 18, 63, 88, 99, 54, 31 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. 18
  2. B. 6
  3. C. 3
  4. D. 36

Correct answer: A. 18

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) = 18, which also equals n!/∏(subtree sizes).

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic