My easy DP version


  • 0
    W
    class Solution {
    public:
        int numTrees(int n) {
            vector< int > tab( n + 1, 0 );
            tab[0] = 1;
            tab[1] = 1;
            
            for(int i = 2; i <= n; ++i) {
                for( int j = 0; j < i; ++j ) {
                    int l = j;
                    int r = i - j - 1;
                    tab[i] += tab[l] * tab[r];
                }
            }
            
            return tab[n];
        }
    };

Log in to reply
 

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