My DP java solution


  • 0
    T

    it's easy once u come up with the recursive version first

    public class Solution {
    public int numTrees(int n) {
        if (n == 0) return 1;
        if (n == 1 ) return 1;
        
        int output[] = new int[n+1];
        output[0] = 1;
        
        for(int nn=1;nn<=n;nn++) {
            for(int i=1;i<=nn;i++) {
                output[nn] += output[i-1]*output[nn-i];    
            }
        }
        
        return output[n];
    
    }
    

    }


Log in to reply
 

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