GATE Computer ScienceVery hardGATE CSE

Keys 1..7 are inserted into an initially empty BST (no balancing) in some order. For how many of the 7! insertion orders does the resulting tree have height 2 (a perfect tree)?

  1. A. 160
  2. B. 40
  3. C. 70
  4. D. 80

Correct answer: D. 80

The first key must be 4; then 2 must precede 1 and 3, and 6 must precede 5 and 7: 6!/(3·3) = 80.

More Algorithms questions

Exams that ask this topic