Created
October 30, 2015 21:57
-
-
Save mengdiwang/51248c6a194a9a46cfbf to your computer and use it in GitHub Desktop.
Unique Binary Search Trees
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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