原题地址: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. IO读 BufferedReader+FileReader

    private static final String FILENAME = "c:\\temp\\in.txt"; public static void main(String[ ...

  2. StringBuffer StringBuilder append

    StringBuilder is not thread safe. So, it performs better in situations where thread safety is not re ...

  3. android studio svn 创建分支

    创建分支或标签 从哪里复制 工作副本 用这个变体去创建分支,并带着当地的改变.通常,服务项将被 添加带历史 , 不仅仅只有目标目录. 每个不同于根的版本文件 ,将被指定的复制.它推荐 去更新 工作副本 ...

  4. BZOJ.3522.[POI2014]Hotel(DP)

    题目链接 BZOJ 洛谷 以为裸点分治,但数据范围怎么这么小?快打完了发现不对.. n^2做的话其实是个水题.. 枚举每一个点为根,为了不重复计算,我们要求所求的三个点必须分别位于三棵子树上. 考虑当 ...

  5. 利用cve-2017-11882的一次渗透测试

    利用工具:https://github.com/Ridter/CVE-2017-11882/ 影响版本: office 2003 office 2007 office 2010 office 2013 ...

  6. 【BZOJ-4031】小z的房间 Matrix-Tree定理 + 高斯消元解行列式

    4031: [HEOI2015]小Z的房间 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 937  Solved: 456[Submit][Statu ...

  7. UVALive 6916 Punching Robot dp

    Punching Robot 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid= ...

  8. 【原】Spring整合Redis(第二篇)—SDR环境搭建具体步骤

    [环境参数] Spring版本:4.2.6.RELEASESpring-Data-Redis版本:1.7.2.RELEASE Redis版本:redis-2.4.5-win32-win64 [简要说明 ...

  9. FireDAC 下的 Sqlite [5] - 数据的插入、更新、删除

    先在空白窗体上添加: TFDConnection.TFDPhysSQLiteDriverLink.TFDGUIxWaitCursor.TFDQuery.TDataSource.TDBGrid(并在设计 ...

  10. STM32 通用定时器相关寄存器

    TIMx_CR1(控制寄存器1) 9-8位:CKD[1:0]时钟分频因子,定义在定时器时钟(CK_INT)频率与数字滤波器(ETR,TIx)使用的采样频率之间的分频比例. 定义:00(tDTS = t ...