Simple python solution


  • 3
    F
    class Solution(object):
    def generateTrees(self, n):
        return self.cal([i for i in xrange(1, n+1)])
        
    def cal(self, lst):
        if not lst: return [None]
        res=[]
        for i in xrange(len(lst)):
            for left in self.cal(lst[:i]):
                for right in self.cal(lst[i+1:]):
                    node, node.left, node.right=TreeNode(lst[i]), left, right
                    res+=[node]
        return res

Log in to reply
 

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