Solution with Dynamic Programming


  • 1
    L
    function numTrees(n) {
        var map = [1].concat(Array(n).fill(0));
        for (var i = 1; i <= n; i++) {
            for (var j = 1; j <= i; j++) {
                map[i] += map[j - 1] * map[i - j];
            }
        }
        return map[n];
    }

Log in to reply
 

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