Java simple solution


  • 0
    public class Solution {
        public int numTrees(int n) {
            int[] dp = new int[n + 2];
            dp[0] = dp[1] = 1;
            for (int len = 2; len <= n; len++) { // length
                for (int j = 1; j <= len; j++) { // j as root
                // j - 1: left child length
                // i - j: right child length
                    dp[len] += dp[j - 1] * dp[len - j];
                }
            }
            return dp[n];
        }
    }
    

Log in to reply
 

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