原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees-ii/

题意:接上一题,这题要求返回的是所有符合条件的二叉查找树,而上一题要求的是符合条件的二叉查找树的棵数,我们上一题提过,求个数一般思路是动态规划,而枚举的话,我们就考虑dfs了。dfs(start, end)函数返回以start,start+1,...,end为根的二叉查找树。

代码:

# Definition for a  binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @return a list of tree node
def dfs(self, start, end):
if start > end: return [None]
res = []
for rootval in range(start, end+1):        #rootval为根节点的值,从start遍历到end
LeftTree = self.dfs(start, rootval-1)
RightTree = self.dfs(rootval+1, end)
for i in LeftTree:                #i遍历符合条件的左子树
for j in RightTree:             #j遍历符合条件的右子树
root = TreeNode(rootval)
root.left = i
root.right = j
res.append(root)
return res
def generateTrees(self, n):
return self.dfs(1, n)

[leetcode]Unique Binary Search Trees II @ Python的更多相关文章

  1. LeetCode: Unique Binary Search Trees II 解题报告

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  2. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  3. LeetCode - Unique Binary Search Trees II

    题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...

  4. LeetCode——Unique Binary Search Trees II

    Question Given an integer n, generate all structurally unique BST's (binary search trees) that store ...

  5. [Leetcode] Unique binary search trees ii 唯一二叉搜索树

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  6. [LeetCode] Unique Binary Search Trees II dfs 深度搜索

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  7. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

  8. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

  9. [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆

    Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...

随机推荐

  1. collectionFramwork-1

    1. Set.List和Map可以看做集合的三大类. List集合是有序集合,集合中的元素可以重复,访问集合中的元素可以根据元素的索引来访问. Set集合是无序集合,集合中的元素不可以重复,访问集合中 ...

  2. 网页图表Highcharts实践教程之标签组与载入动画

    网页图表Highcharts实践教程之标签组与载入动画 Highcharts标签组 在图表的大部分元素都提供了标签功能.但很多时候,我们需要额外说明一些信息.这个时候借助原有的图表元素的标签功能就不是 ...

  3. HTML基础单页面总结(基于w3school教程)

    学习了一阵http://www.w3school.com.cn网站上的html教程,发现各个知识点比较分散,个人比较倾向于用一页html文档就把所有涉及的基本html标签元素知识点都展示出来的形式.个 ...

  4. # 2017-2018-20172309 暑期编程作业:APP

    2017-2018-20172309 暑期编程作业:基于有道词典API的翻译软件的实现. 写在前面:这个博客可以说是拖了很久了.因为做这个APP已经很久了,很多东西都已经忘记了,所以一直都懒得写.但是 ...

  5. BZOJ 4636: 蒟蒻的数列 分块

    4636: 蒟蒻的数列 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4636 Description 蒟蒻DCrusher不仅喜欢玩扑克 ...

  6. @Transactional导致无法动态数据源切换

    公司目前数据源为主从模式:主库可读写,从库只负责读.使用spring-jdbc提供的AbstractRoutingDataSource结合ThreadLocal存储key,实现数据源动态切换. 最近项 ...

  7. clientHeight ,offsetHeight,style.height,scrollHeight有区别与联系

    style.height 包括 元素的滚动条,不包括边框clientHeight 不包括元素的滚动条和边框,只有在没有元素的滚动条的情况下,style.height相等于clientHeightoff ...

  8. oracle 11g wm_concat 、 listagg 函数的使用(合并数据)

    方法一 wn_concat() 函数 1.把以下图中Name一样的数据合并为一条,而且NO的值要这样显示如 C.1,C.2 2.实现这种效果的操作如下,先把Name的值进行分组(group by),再 ...

  9. VC设置代理方法

      参考文章: VC 设置代理 Setting and Retrieving Internet Options Change Internet Proxy settings http://suppor ...

  10. 华为正在力挺的NB-IoT是什么鬼! - 全文

    NB-IoT,Niubility Internet of Thing,即牛掰的物联网技术. 关于物联网,小编想从2款很有趣的应用说起. 这不是在播限制级.这是Nake Labs推出的3D健身镜,这款智 ...