6 Lines solution by bottom up


  • 0
    X
        int numTrees(int n) 
        {
            vector<int> table(n+1,0);
            table[0]=1,table[1]=1;
            for(int i=2;i<=n;i++)
                for(int j=0;j<=n-1;j++)
                    table[i]+=table[j]*table[i-j-1];
            return table[n];
        }
    

Log in to reply
 

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