GATE Computer ScienceVery hardGATE CS (PYQ pattern)

Keys 1 to 6 are inserted into an empty BST in each of the 720 possible orders. For how many orders is the height of the resulting tree exactly 4 (a single node has height 0)?

  1. A. 32
  2. B. 208
  3. C. 400
  4. D. 240

Correct answer: B. 208

Height 5 (a path) needs every inserted key to be the current minimum or maximum: 2^5 = 32 orders. Height 4 means the longest root-to-leaf path has 5 nodes with exactly one key off it; enumerating all 720 orders gives the distribution height 2: 80, 3: 400, 4: 208, 5: 32.

More Programming and Data Structures (GATE) questions

Exams that ask this topic