BST的个数以及所有解的构造

int numTrees(int n) {
        if(n<=0) return 0;
        vector dp(n+1,0);
        dp[0] = 1;
        dp[1] = 1;
        for(int i=2;i<=n;i++){  //node个数
            for(int j=1;j<=i;j++){  //left的个数
                dp[i] += dp[j-1]*dp[i-j];
            }
        }
        return dp[n];
    }
public class Solution {
    public ArrayList generateTrees(int n) {
        return helper(1,n);
    }
    
    public ArrayList helper(int beg,int end){
        ArrayList res = new ArrayList();
        if(beg>end){
            res.add(null);
            return res;
        }
        for(int i=beg;i<=end;i++){  //枚举中间点
            ArrayList left = helper(beg,i-1);
            ArrayList right = helper(i+1,end);
            for(int j=0;j


你可能感兴趣的:(dfs,dp)