class Solution {
public:
int numTrees(int n) {
if(n<2) return 1;
int *dp=new int[n+1];
int i,j,k;
dp[0]=dp[1]=1;
for(i=2;i<=n;++i)
{
dp[i]=0;
for(j=0,k=i-1;j<k;++j,--k)
{
dp[i]+=2*dp[j]*dp[k];
}
if(k==j) dp[i]+=dp[j]*dp[k];
}
return dp[n];
}
};
leetcode - 96. Unique Binary Search Trees
最新推荐文章于 2019-05-09 17:47:01 发布