Data Structures & AlgorithmsVery hardGATE CS

Keys 56, 16, 21, 93, 86, 85, 15 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. 40
  2. B. 20
  3. C. 2
  4. D. 80

Correct answer: A. 40

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic