GATE Computer ScienceVery hardGATE CS (PYQ pattern)

A BST is built by inserting 50, 30, 70, 20, 40, 60, 80 in some order into an initially empty tree. The final tree is the perfectly balanced 3-level tree. How many of the 7! insertion orders produce exactly this tree?

  1. A. 160
  2. B. 210
  3. C. 720
  4. D. 80

Correct answer: D. 80

The root must come first. After that, the left subtree's 3 keys and the right subtree's 3 keys can interleave in C(6,3) = 20 ways, and in each subtree the middle key must precede its children (2 ways each): 20 * 2 * 2 = 80.

More Programming and Data Structures (GATE) questions

Exams that ask this topic