题目链接
https://leetcode.com/problems/unique-binary-search-trees-ii/
题目原文
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.
For example,
Given n = 3, your program should return all 5 unique BST's shown below.
题目大意
给出一个数字n,用1-n构建二叉查找树,返回所有符合条件的二叉树
解题思路
使用dsf进行求解(求个数一般使用递归,若枚举可考虑使用dfs)
代码
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def dfs(self, start, end):
if start > end:
return [None]
ans = []
for val in range(start, end + 1):
leftTree = self.dfs(start, val - 1)
rightTree = self.dfs(val + 1, end)
for l in leftTree:
for r in rightTree:
root = TreeNode(val)
root.left = l
root.right = r
ans.append(root)
return ans
def generateTrees(self, n):
"""
:type n: int
:rtype: List[TreeNode]
"""
if n == 0:
return []
return self.dfs(1, n)