4 lines c++ solution.


  • 0
    A
    int numTrees(int n) {
            long long result=1;
            for(int i=n+1;i<=2*n;i++){
                result = (result * i)/(i-n);
            }
            return result/(n+1);
    }
    

    The total number of possible tree's is given by the formula (2n)!/(n+1)!*n! which is called the Catalan number. Read more about it here.


Log in to reply
 

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