O(n2) java solution


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

Log in to reply
 

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