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
- Catalan numbers count balanced bracket strings, binary tree shapes, and more.
- C(0) = 1, and C(i) = Σ C(j) · C(i − 1 − j) for j from 0 to i − 1.
- Fill the table from small i upwards, reusing earlier answers (dynamic programming).
- The sequence begins 1, 1, 2, 5, 14, 42, 132, …
Watch it run
Loading animation…