【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 ...
随机推荐
- leetcode刷题之数组NO.4
1.题目 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数 ...
- Flink 实践教程-进阶(2):复杂格式数据抽取
作者:腾讯云流计算 Oceanus 团队 流计算 Oceanus 简介 流计算 Oceanus 是大数据产品生态体系的实时化分析利器,是基于 Apache Flink 构建的具备一站开发.无缝连接.亚 ...
- Vue3 中有哪些值得深究的知识点?
众所周知,前端技术一直更新很快,这不 vue3 也问世这么久了,今天就来给大家分享下vue3中值得注意的知识点.喜欢的话建议收藏,点个关注! 1.createApp vue2 和 vue3 在创建实例 ...
- 日常Java 2021/11/9
线程的优先级 每一个Java线程都有一个优先级,这样有助于操作系统确定线程的调度顺序.Java线程的优先级是一个整数,其取值范围是1(Thread.MIN_PRIORITY ) -10 (Thread ...
- Flink(八)【Flink的窗口机制】
目录 Flink的窗口机制 1.窗口概述 2.窗口分类 基于时间的窗口 滚动窗口(Tumbling Windows) 滑动窗口(Sliding Windows) 会话窗口(Session Window ...
- Linux基础命令---nslookup查询域名工具
nslookup nslookup是一个查询DNS域名的工具,它有交互和非交互两种工作模式. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora. 1.语法 ...
- linux如何安装缺失依赖
这里要提到一个网站https://pkgs.org/,他是linux系统的一个相关网站,里面都是相关内容 Warning: RPMDB altered outside of yum. ** Found ...
- Vue中如何书写js来渲染页面填充数据的部分代码
new Vue({ el:"#app" , data:{ user:{ id:"", username:"", password:" ...
- java 对 final 关键字 深度理解
基础理解 : 1.修饰类 当用final去修饰一个类的时候,表示这个类不能被继承.处于安全,在JDK中,被设计为final类的有String.System等,这些类不能被继承 .注意:被修饰的类的成员 ...
- matplotlib画散点图和柱状图,等高线图,image图
一:散点图: scatter函数原型 其中散点的形状参数marker如下: 其中颜色参数c如下: n = 1024 # 均值是0, 方差是1, 取1024个数 x = np.rando ...