Skip to content
StackDecode
Number theory & mathAnimated

Catalan Numbers

C(n) counts balanced brackets, binary trees and more.

Time

O(n²) DP, O(n) formula

Extra space

O(n)

Use it for

Counting nested structures.

How it works

  1. Catalan numbers count balanced bracket strings, binary tree shapes, and more.
  2. C(0) = 1, and C(i) = Σ C(j) · C(i − 1 − j) for j from 0 to i − 1.
  3. Fill the table from small i upwards, reusing earlier answers (dynamic programming).
  4. The sequence begins 1, 1, 2, 5, 14, 42, 132, …

Watch it run

Loading animation…