96. 不同的二叉搜索树

package leetcode;


public class demo_96 {
    public int numTrees(int n) {
        int[] dp=new int[n+1];
        dp[0]=1;
        dp[1]=1;
        //按每一个数作为一次根节点,累计左右子树的可能性
        for(int i=2;i<=n;i++) {
            for(int j=1;j<=i;j++) {
                dp[i]=dp[i]+dp[j-1]*dp[i-j];
            }
        }
        System.out.println(dp[n]);
        return dp[n];
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        demo_96 demo=new demo_96();
        demo.numTrees(3);
    }
}

 

posted on 2022-05-06 10:16  一仟零一夜丶  阅读(18)  评论(0)    收藏  举报