递归

1、95. 不同的二叉搜索树 II

考点:

递归思想

# 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)

  

posted @ 2020-12-10 11:10  哈哈哈喽喽喽  阅读(68)  评论(0编辑  收藏  举报