[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]* ...
随机推荐
- 建筑的永恒之道 (C·亚历山大 著)
永恒之道 建筑或城市只有踏上了永恒之道,才会生机勃勃. 第1章 永恒之道 它是一个唯有我们自己才能带秩序的过程,它不可能被求取,但只要我们顺应它,它便会自然而然地出现. 质 为了探求永恒之道,我们首先 ...
- 策略模式(Strategy )
为实现一个目的采用不同的方式都可实现,具体看要采取哪种方式. //接口 public interface Strategy { public void algorithmInterface(); ...
- 如何用原生js开发一个Chrome扩展程序
原文地址:How to Build a Simple Chrome Extension in Vanilla JavaScript 开发一个Chrome扩展程序非常简单,只需要使用原生的js就可以完成 ...
- ES6 基本语法
ECMAScript 6 简介 ECMAScript 6.0(以下简称 ES6)是 JavaScript 语言的下一代标准,已经在 2015 年 6 月正式发布了.它的目标,是使得 JavaScrip ...
- UDP广播包
一,广播地址: 广播地址是专门用于同时向网络中所有工作站进行发送的一个地址.在使用TCP/IP 协议的网络中,主机号为全1的IP地址为广播地址.例如,对于 :192.168.199.0(掩码:255. ...
- 【python】脚本连续发送QQ邮件
今天习得用python写一个连续发送QQ邮件的脚本,经过测试,成功给国内外的服务器发送邮件,包括QQ邮箱.163邮箱.google邮箱,香港科技大学的邮箱和爱丁堡大学的邮箱.一下逐步解答相关技巧. 首 ...
- Java12配置
配置环境变量: 之前的JAVA_HOME和CLASSPATH已经都不要了.只要配置jdk的bin到Path里: C:\Program Files\Java\jdk-12\bin
- SqlServer查询某个表的列名称、说明、备注、类型等
SELECT 表名 = case when a.colorder=1 then d.name else '' end, 表说明 = case when a.colorder=1 then isnull ...
- [UE4]Tile View
一.Tile View也属于List View,Tile View以小方格的形式展示子控件. 二.Tile View.Entry Height.Tile View.Entry Width设置每个Til ...
- java内存区域之程序计数器
程序计数器(program counter register) 作用:字节码解释其工作时,通过这个计数器的值的改变,来选取下一条执行的字节码命令. 由于java虚拟机的都线程是通过线程轮流切换,并分配 ...