Simple Python Solution using the Catalan Formula


  • 0
    U

    Catalan Formula = 2n!/(n+1!*n!)

    class Solution(object):
        def numTrees(self, n):
            facn=1
            for i in range(1,n+1):
                facn *= i
            fac2n=facn
            facnp=facn*(n+1)
            for i in range(n+1,2*n+1):
                fac2n *= i
            ans = fac2n/(facn*facnp)
            return ans

Log in to reply
 

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