1 class Solution: 2 def generateTree(self,s,e) -> 'List[TreeNode]': 3 re = [] 4 if s > e: 5 re.append(None) 6 return re 7 for i in range(s,e+1): 8 left = self.generateTree(s,i-1) 9 right = self.generateTree(i+1,e) 10 for l in left: 11 for r in right: 12 root = TreeNode(i) 13 root.left = l 14 root.right = r 15 re.append(root) 16 return re 17 18 def generateTrees(self, n: int) -> 'List[TreeNode]': 19 if n < 1: 20 return list() 21 return self.generateTree(1,n)
分治法