Data Structures & AlgorithmsVery hardGATE CS · Advanced

How many structurally different binary search trees can be built on the keys 1, 2, ..., 8 (all distinct shapes, one per key set) whose height, counted in edges on the longest root-to-leaf path, is exactly 4?

  1. A. 470
  2. B. 480
  3. C. 94
  4. D. 376

Correct answer: D. 376

Let c(n, H) count shapes of height at most H via c(n,H) = sum c(i,H-1) c(n-1-i,H-1). Exactly 4 is c(8,4) - c(8,3) = 470 - 94 = 376.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic