DP solution,definitely many peoples solve this problem using DP, So just to remind me later:)


  • 0
    Z
    class Solution {
    public:
        int numTrees(int n) {
            if(n<=0) return 0;
            int all[n+1];
            all[0] = 1;
            all[1] = 1;
            for(int i=1;i<=n;++i)
            {
                all[i] = 0;
                for(int j=1;j<=i;++j)
                    all[i] += (all[i-j] * all[j-1]); 
            }
            return all[n];
        }
    };

Log in to reply
 

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