递归
考点:
递归思想
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def generateTrees(self, n: int) -> List[TreeNode]: if n == 0: return [] def digui(start, end): if start > end: return [None] result = [] for i in range(start, end+1): lefts = digui(start, i-1) rights = digui(i+1, end) for j in lefts: for k in rights: cur = TreeNode(i) cur.left = j cur.right = k result.append(cur) return result return digui(1, n)