题目连接:Leetcode 096 Unique Binary Search Trees
解题思路:dp[i]表示i个结点构成不同的二叉树有多少种,dp[i] = dp[i-j-1] * dp[j] (j < i)。
class Solution {
public:
int numTrees(int n) {
if (n == 0) return 0;
int* dp = new int[n+1];
dp[0] = 1;
for (int i = 1; i <= n; i++) {
dp[i] = 0;
for (int j = 1; j <= i; j++)
dp[i] += dp[j-1] * dp[i-j];
}
int ans = dp[n];
delete[] dp;
return ans;
}
};