[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 ... n.
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
题意:
给定n个节点,列举可形成的不同的BST集合
思路:
跟 [leetcode]96. Unique Binary Search Trees给定节点形成不同BST的个数 相似。
对于给定的n
需要去查[1, n]范围内可生成的BST,如下图,
1 1 2 3 3 ... i ... n
\ \ / \ / / / \
3 2 1 3 2 1 [1,i-1] [i+1,n]
/ \ / \
2 3 1 2
我们需要列出
以1为root可生成的所有BST
以2为root可生成的所有BST
......
那么对于任意以 i 为root可生成的所有BST,根据BST的性质:
其左子树的值一定小于i,也就是[1, i - 1]区间,用helper生成list of leftList
而右子树的值一定大于i,也就是[i + 1, n]区间, 用helper生成list of rightList
最后,用root, leftList中的值,rightList中的值,三者生成BST
代码:
class Solution {
public List<TreeNode> generateTrees(int n) {
if(n == 0) return new ArrayList<>();
return helper(1, n); // root node from 1 to n
}
private List<TreeNode> helper(int left, int right){
List<TreeNode> result = new ArrayList<>();
if(left > right){
result.add (null);
return result;
}
for(int i = left; i <= right; i++){
List<TreeNode> lefts = helper(left, i-1);
List<TreeNode> rights = helper(i+1, right);
for(TreeNode l : lefts){
for(TreeNode r : rights){
TreeNode root = new TreeNode(i);
root.left = l;
root.right = r;
result.add(root);
}
}
}
return result;
}
}
[leetcode]95. Unique Binary Search Trees II给定节点形成不同BST的集合的更多相关文章
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- [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 ----- 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 (唯一二叉搜索树) 解题思路和方法
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 (Medium)
原题 字母题添加链接描述 一开始完全没有思路.. 百度看了别人的思路,对于这种递归构造的题目还是不熟,得多做做了. 这个题目难在构造出来.一般构造树都需要递归. 从1–n中任意选择一个数当做根节点,所 ...
- LeetCode 95. Unique Binary Search Trees II 动态演示
比如输入为n, 这道题目就是让返回由1,2,... n的组成的所有二叉排序树,每个树都必须包含这n个数 这是二叉树的排列组合的题目.排列组合经常用DFS来解决. 这道题比如输入为3,也就是求start ...
- 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)
[LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
- leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses
96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...
随机推荐
- PythonStudy——第一阶段性测试
1.Python解释器,在2.x和3.x上分别采用的是什么默认编码8 2.定义字符串变量时,单引号,双引号,三引号什么区别? 3.编程语言可以分为哪三类,特点都是什么 4.定义一个变量有三个特性, 5 ...
- fromkeys()
#fromkeys() #说明:用于创建一个新字典,以序列seq中元素做字典的键,value为字典所有键对应的初始值 #案例 d=[1,2,3] dict={} dict=dict.fromkeys( ...
- 使用openresty && minio && thumbor 构建稳定高效的图片服务器
备注: minio 是一个开源的s3 协议兼容的分布式存储,openresty nginx+lua 高性能可扩展的nginx 衍生版,thumbor 基于python 的图片处理服务器,支持图片的裁剪 ...
- Docker组件与元素(三)
说明: 这篇博文是根据国外的另一篇总结而来,第一个链接为原文,第二个为译文,第三个有几个图挺好 http://blog.flux7.com/blogs/docker ...
- mysql查询中AND与OR注意事项
在查询的where条件中,and要优于or 如果要改变优先级, 需要在最小逻辑判断的条件外加括号(),例如: select * from `table_name` where (`type` = 1 ...
- System.Object
Object():System.Object类型的构造函数,自动调用. ~Object()/Finalize():System.Object类型的祈构函数,自动调用且不能够手动. Equals(obj ...
- chrome's developer console
原文链接: https://medium.freecodecamp.org/10-tips-to-maximize-your-javascript-debugging-experience-b69a7 ...
- ROS Qt Creator Plug-in wiki
在Qt中配置ros工程. 环境: ubuntu16.04: ros kinetic: Qt5.7 参考网址: https://ros-industrial.github.io/ros_qtc_plug ...
- js源生ajax
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Java异常学习总结二
异常的处理方式 方式一:捕获异常(try-catch-finally) 捕获异常是通过三个关键词来实现的:try-catch-finally.用try来执行一段程序,如果出现异常,系统抛出一个异常,可 ...