Skip to content

Latest commit

 

History

History
44 lines (33 loc) · 1.05 KB

p0095M-genetateTrees.md

File metadata and controls

44 lines (33 loc) · 1.05 KB

Problem: 95. 不同的二叉搜索树 II

[TOC]

思路

不会,看的答案。。。

Code

# 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[Optional[TreeNode]]:
        def dfs(start,end):
            allTrees =[]
            if start > end :
                return [None,]

            for i in range(start,end+1):
                lefttree = dfs(start,i-1)
                righttree = dfs(i+1,end)

                for l in lefttree:
                    for r in righttree:
                        currTree = TreeNode(i)
                        currTree.left = l
                        currTree.right = r
                        allTrees.append(currTree)
            return allTrees
        if n == 0:
            return []
        else:
            return dfs(1,n)