Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Leetcode #96: Unique Binary Search Trees
class Solution {
public:
int numTrees(int n) {
vector<int> dp(n+1);
dp[0] = 1;
dp[1] = 1;
for (int i=2; i<=n; i++) {
int sum = 0;
for (int k=1; k<=i; k++) {
sum += dp[k-1]*dp[i-k];
}
dp[i] = sum;
}
return dp[n];
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.