Simple recursive python solution with dict


  • 0
    L
    class Solution(object):
        def numTrees(self, n):
            """
            :type n: int
            :rtype: int
            """
            self.cache={}
            def getN(n):
                if n in self.cache:
                    return self.cache[n]
                res=0 if n else 1
                for i in range(1,n+1):
                    res+=(getN(i-1)*getN(n-i))
                self.cache[n]=res
                return res
            return getN(n)
    

Log in to reply
 

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