Skip to content
Mad4Study

Engineering & Science

GATE Computer Science

  • GATE CS
  • 2026–27 pattern practice
  • Algorithms
  • Dynamic programming
  • Medium

How many structurally different binary search trees can be built with 4 distinct keys?

Algorithm Design

Pick an option to check your answer.

The count is the Catalan number Cn=1n+1(2nn)C_n = \dfrac{1}{n+1}\dbinom{2n}{n}.

With root kk, the left subtree has k−1k - 1 keys and the right has n−kn - k, so Cn=∑k=1nCk−1Cn−kC_n = \sum_{k=1}^{n} C_{k-1}C_{n-k}.

C0=1C_0 = 1, C1=1C_1 = 1, C2=2C_2 = 2, C3=5C_3 = 5, and C4=C0C3+C1C2+C2C1+C3C0=5+2+2+5=14C_4 = C_0C_3 + C_1C_2 + C_2C_1 + C_3C_0 = 5 + 2 + 2 + 5 = 14.

Why not the others

  • 2424 counts the orderings of 4 keys (4!4!); different insertion orders can give the same BST.
  • 1616 is 242^4, which has no link to BST shapes.
  • 1010 drops some shapes; the recurrence gives 14.

Report an error

GATE CS · Algorithms · Algorithm Design