Skip to content

Instantly share code, notes, and snippets.

@mengdiwang
Created October 30, 2015 21:57
Show Gist options
  • Save mengdiwang/51248c6a194a9a46cfbf to your computer and use it in GitHub Desktop.
Save mengdiwang/51248c6a194a9a46cfbf to your computer and use it in GitHub Desktop.
Unique Binary Search Trees
class Solution {
public:
int numTrees(int n) {
if(n<=1) return 1;
int *a = new int[n+1];
memset(a, 0, sizeof(int)*(n+1));
a[1] = 1;
a[0] = 1;
for(int i=2; i<=n; i++)
{
for(int j=1; j<=i; j++)
{
a[i] += (a[j-1] * a[i-j]);
}
}
int ret = a[n];
delete []a;
return ret;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment