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.
class Solution {
public:
int numTrees(int n) {
int a[n+1];
int i,j;
memset(a,0,sizeof(a));
a[0]=1;
a[1]=1;
for(i=2;i<=n;i++)
for(j=1;j<=i;j++)
{
a[i]+=a[j-1]*a[i-j];
}
return a[n];
}
};
看了阿三哥的视频才做出来 http://www.youtube.com/watch?v=UfA_v0VmiDg