I used the formula discussed in:

http://www.quora.com/Given-n-how-many-structurally-unique-BSTs-binary-search-trees-that-store-values-1-to-n-are-there

```
class Solution {
public:
int numTrees(int n)
{
if(n == 0)
return 0;
vector<int> nums(n + 1, 0);
nums[0] = 1;
for(int i = 1; i <= n; i++)
{
int num = 0;
for(int k = 1; k <= i; k++)
{
num += nums[k - 1] * nums[i - k];
}
nums[i] = num;
}
return nums.back();
}
};
```