Given n, how many structurally unique BST's (binary search trees) that store values 1...n?
For example,
Given n = 3, there are a total of 5 unique BST's.
1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3
int C(int n, int m)
{
int ans = 1;
int div = 2;
for (int i = 0; i < m; ++i)
{
ans *= (n - i);
while (div <= m)
{
if (ans % div == 0)
{
ans /= div;
div++;
}
else
{
break;
}
}
}
return ans;
}
count = C(2*n, n) / (n + 1);