30:00
Given an integer n, return the number of structurally unique binary search trees that store exactly the values 1 to n, each value used once.
1≤n≤191 \leq n \leq 191≤n≤19
Run code to see results.
G[k] will count BST shapes on k keys. Only the count of keys matters, not their values. G[0] = 1: the empty tree is one shape.
Count shapes, not values
left subtree
root
right subtree
A BST on any k sorted keys has the same number of shapes as one on keys 1..k.
G[k] = shapes on k keys
0
1
2
3
Given n, count the structurally different binary search trees that hold exactly the values 1 to n. Only the shape of the tree matters, since the BST rule then fixes where every value goes.