【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 ...
随机推荐
- ubuntu终端ls颜色配置
buntu中没有LS_COLORS,/etc/目录中也没有DIR_COLORS,所以这里使用dircolor命令加以解决 1. 利用dircolors命令,查看我们的系统当前的文件名称显示颜色的值,然 ...
- 论文翻译:2020_Weighted speech distortion losses for neural-network-based real-time speech enhancement
论文地址:基于神经网络的实时语音增强的加权语音失真损失 论文代码:https://github.com/GuillaumeVW/NSNet 引用:Xia Y, Braun S, Reddy C K A ...
- day27 网络编程
1.OSI七层协议 1.七层划分为:应用层,表示层.会话层.传输层.网络层.数据链路层.物理层 2.五层划分:应用层.传输层.网络层.数据链路层.物理层 应用层: 表示层: 会话层: 传输层:四层交换 ...
- 【MPI环境配置】 vs2019配置MPI环境
MPI 即 Message-Passing Interface,提供了一系列并行编程的接口,为了在本机能够学习和使用并行编程,需要提前安装MPI; 配置环境: Microsoft Visual Stu ...
- android studio 使用 aidl(二)异步回调
基础使用请移步 android studio 使用 aidl (一) 首先建立在server端建立两个aidl文件 ITaskCallback.aidl 用于存放要回调client端的方法 // IT ...
- linux安装redis报错
问题:You need tcl 8.5 or newer in order to run the Redis test 解决办法: wget http://downloads.sourceforge. ...
- node环境变量配置
1.Node.js 官方网站下载:https://nodejs.org/en/ 2.打开安装,傻瓜式下一步即可,然后配置环境变量 3.因为在执行例如npm install webpack -g等命令全 ...
- 收集linux网络配置信息的shell脚本
此脚本已在CentOS/ RHEL和Fedora Linux操作系统下进行测试过.可用于当前网络配置信息. 代码: #!/bin/bash # HWINF=/usr/sbin/hwinfo IFCFG ...
- 【Linux】【Services】【DNS】使用Bind搭建DNS服务
1. 简介 1.1. 实现的功能:DNS解析以及智能转发 1.2. 官方文档: 1.3. 基础概念:http://www.cnblogs.com/demonzk/p/6494968.html 2. 环 ...
- 【Linux】【Shell】【Basic】文件查找locate,find
1.locate: 1.1. 简介:依赖于事先构建好的索引库: 系统自动实现(周期性任务): 手动更新数据库(updatedb): 1.2. 工作特性:查找速度快:模糊 ...