GATE Computer ScienceVery hardGATE CS (PYQ pattern)

Keys 50, 30, 70, 20, 40, 60, 80 are inserted in this order into an empty binary search tree. How many of the 7! insertion orders of these keys produce exactly the same tree?

  1. A. 48
  2. B. 80
  3. C. 120
  4. D. 720

Correct answer: B. 80

The root 50 must come first. The left subtree {30, 20, 40} needs 30 before 20 and 40: 2 valid orders; the right subtree likewise 2. Interleaving the 3 left keys and 3 right keys: C(6, 3) = 20. Total 20·2·2 = 80 (brute force over all permutations agrees).

More Algorithms questions

Exams that ask this topic