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); } }
浙公网安备 33010602011771号