GATE Computer ScienceVery hardGATE CS

How many binary search trees on the 7 keys 1 to 7 have height exactly 4 (height counted in edges, a single node has height 0)?

  1. A. 144
  2. B. 152
  3. C. 68
  4. D. 129

Correct answer: B. 152

Let B(n, h) be the number of BSTs on n keys with height at most h and count by choosing the root. Then the number with height exactly 4 is B(7,4) − B(7,3) = 152. As a check, the totals for heights 2 to 6 are 1, 68, 152, 144, 64, which add up to the Catalan number C₇ = 429.

More Algorithms questions

Exams that ask this topic