【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)
【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)
标签(空格分隔): LeetCode
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.me/
题目地址:https://leetcode.com/problems/unique-binary-search-trees-ii/description/
题目描述:
Given an integer n, generate all structurally unique BST’s (binary search trees) that store values 1 ... n
.
Example:
Input: 3
Output:
[
[1,null,3,2],
[3,2,null,1],
[3,1,null,null,2],
[2,1,3],
[1,null,2,null,3]
]
Explanation:
The above output corresponds to the 5 unique BST's shown below:
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
题目大意
输出用1–n这几个数字能组成的所有BST.
解题方法
这个题目又是基于之前的96. Unique Binary Search Trees改进版的题目。之前的题目的做法是只用求出有多少组BST即可,做法是卡特兰数。
这个题目难在构造出来。一般构造树都需要递归。从1–n中任意选择一个数当做根节点,所以其左边的数字构成其左子树,右边的数字当做右子树。因为要求出所有的子树,所以当左子树固定的时候,把所有可能的右子树都构成,然后再变换左子树。
这个代码难理解的地方在于left_nodes 和 right_nodes的求法,这个一定要结合递归的终止条件去看,当选择的根节点的值i比left小的时候,那么其实左子树就是空了。如果把这个理解了,那么下面的对左右子树的遍历应该也不难理解。
# 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 generateTrees(self, n):
"""
:type n: int
:rtype: List[TreeNode]
"""
if n == 0: return []
return self.generateTreesDFS(1, n)
def generateTreesDFS(self, left, right):
if left > right:
return [None]
res = []
for i in range(left, right + 1):
left_nodes = self.generateTreesDFS(left, i - 1)
right_nodes = self.generateTreesDFS(i + 1, right)
for left_node in left_nodes:
for right_node in right_nodes:
root = TreeNode(i)
root.left = left_node
root.right = right_node
res.append(root)
return res
日期
2018 年 6 月 22 日 ———— 这周的糟心事终于完了
【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)的更多相关文章
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- LeetCode: Unique Binary Search Trees II 解题报告
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- [leetcode]95. Unique Binary Search Trees II给定节点形成不同BST的集合
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- [LeetCode] 95. Unique Binary Search Trees II 唯一二叉搜索树 II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- [LeetCode] 95. Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- leetCode 95.Unique Binary Search Trees II (唯一二叉搜索树) 解题思路和方法
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- leetcode 95 Unique Binary Search Trees II ----- java
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- [leetcode]95 Unique Binary Search Trees II (Medium)
原题 字母题添加链接描述 一开始完全没有思路.. 百度看了别人的思路,对于这种递归构造的题目还是不熟,得多做做了. 这个题目难在构造出来.一般构造树都需要递归. 从1–n中任意选择一个数当做根节点,所 ...
- LeetCode 95. Unique Binary Search Trees II 动态演示
比如输入为n, 这道题目就是让返回由1,2,... n的组成的所有二叉排序树,每个树都必须包含这n个数 这是二叉树的排列组合的题目.排列组合经常用DFS来解决. 这道题比如输入为3,也就是求start ...
随机推荐
- 【模板】有源汇有上下界最大流(网络流)/ZOJ3229
先导知识 无源汇有上下界可行流 题目链接 https://vjudge.net/problem/ZOJ-3229 https://www.luogu.com.cn/problem/P5192 (有改动 ...
- c#年份筛选
年份: <script type="text/javascript" src="http://www.shicishu.com/down/WdatePicker.j ...
- 日常Java 2021/11/9
线程的优先级 每一个Java线程都有一个优先级,这样有助于操作系统确定线程的调度顺序.Java线程的优先级是一个整数,其取值范围是1(Thread.MIN_PRIORITY ) -10 (Thread ...
- day12 函数嵌套
day12 函数嵌套 一. args与kwargs def index(a,b,c): print(a,b,c) def wrapper(*args,**kwargs): # args=(1,2,3) ...
- Zookeeper【概述、安装、原理、使用】
目录 第1章 Zookeeper入门 1.1 概述 1.2 特点 1.3 数据结构 1.4应用场景 第2章 Zookeep安装 2.1 下载地址 2.2 本地模式安装 1. 安装前准备 2. 配置修改 ...
- Vue相关,diff算法。
1. 当数据发生变化时,vue是怎么更新节点的? 要知道渲染真实DOM的开销是很大的,比如有时候我们修改了某个数据,如果直接渲染到真实dom上会引起整个dom树的重绘和重排,有没有可能我们只更新我们修 ...
- ES6必知,变量的结构赋值。
对象和数组时 Javascript 中最常用的两种数据结构,由于 JSON 数据格式的普及,二者已经成为 Javascript 语言中特别重要的一部分. 在编码过程中,我们经常定义许多对象和数组,然后 ...
- mysql explain using index condition
Using where:表示优化器需要通过索引回表查询数据:Using index:表示直接访问索引就足够获取到所需要的数据,不需要通过索引回表:Using index condition:在5.6版 ...
- ebs 初始化登陆
BEGIN fnd_global.APPS_INITIALIZE(user_id => youruesr_id, esp_id => yourresp_id, resp_appl_id = ...
- hive 启动不成功,报错:hive 启动报 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/mapred/MRVersi
1. 现象:在任意位置输入 hive,准备启动 hive 时,报错: Exception in thread "main" java.lang.NoClassDefFoundErr ...