My DP solution for 96. Unique Binary Search Trees


  • 0
    B
       public int numTrees(int n) {
            if (n<=2) {
    			return n;
    		}
    		int [] nums = new int [n+1];
    		nums[0] = 1;
    		nums[1]  =1;
    		nums[2] = 2;
    		for(int i=3;i<=n;i++){
    			for(int j=0;j<=i-1;j++){
    				nums[i]+=nums[j]*nums[i-1-j];
    			}
    		}
    		return nums[n];
        }

  • 0
    S

    You don't need these base cases
    nums[1] =1;
    nums[2] = 2;


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.