Unique Binary Search Trees I & II
Given n, how many structurally unique BSTs (binary search trees) that store values 1...n?
Given n = 3, there are a total of 5 unique BST's.
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
当数组为 1,2,3,4,.. i,.. n时,基于以下原则的BST建树具有唯一性:以i为根节点的树,其左子树由[0, i-1]构成, 其右子树由[i+1, n]构成。那么,左子树由[0 , i -1]组成时,又有多少组合方式呢?这就可以递归了嘛。
public class Solution {
public int numTrees(int n) {
int[] result = new int[n + ];
result[] = result[] = ;
for (int i = ; i <= n; ++i) {
for (int j = ; j < i; ++j) { // j refers to the number of nodes in the left subtree
result[i] += result[j] * result[i - j - ];
}
}
return result[n];
}
}
Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.
Given n = 3, your program should return all 5 unique BST's shown below.
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @paramn n: An integer
* @return: A list of root
*/
public ArrayList<TreeNode> generateTrees(int n) {
return generate(, n);
} private ArrayList<TreeNode> generate(int start, int end){
ArrayList<TreeNode> treeList = new ArrayList<>(); if(start > end){
treeList.add(null);
return treeList;
} for(int i = start; i <= end; i++){
ArrayList<TreeNode> left = generate(start, i-);
ArrayList<TreeNode> right = generate(i+, end);
for(TreeNode l: left){
for(TreeNode r: right){
TreeNode root = new TreeNode(i);
root.left = l;
root.right = r;
treeList.add(root);
}
}
}
return treeList;
}
}
Unique Binary Search Trees I & II的更多相关文章
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
- leetcode -day28 Unique Binary Search Trees I II
1. Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search t ...
- Unique Binary Search Trees I&&II(II思路很棒)——动态规划(II没理解)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For exa ...
- Unique Binary Search Trees I&II——给定n有多少种BST可能、DP
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 【LeetCode】95. Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 【leetcode】Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 41. Unique Binary Search Trees && Unique Binary Search Trees II
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
- LeetCode: Unique Binary Search Trees II 解题报告
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- Unique Binary Search Trees,Unique Binary Search Trees II
Unique Binary Search Trees Total Accepted: 69271 Total Submissions: 191174 Difficulty: Medium Given ...
随机推荐
- 问问题_为什么关闭浏览器后Session会失效
首先需要理解一下几点: 1.Http是无状态的,即对于每一次请求都是一个全新的请求,服务器不保存上一次请求的信息 2.Session是保存在服务端的,为什么后续请求会读取到session?因为请求会包 ...
- js-定时任务setInterval,setTimeout,clearInterval,clearTimeout
setInterval()循环执行相应的方法 <script type="text/javascript"> setInterval("myInterval( ...
- yii2URL美化
yii2的url 域名/index.php?r=site%2Findex 实际为 域名/index.php?r=site/index 可以美化下 可以在main.php中配置 'components' ...
- groovy-输入输出
Groovy为I/O提供了一系列的helper methods ,所有的这些方法都适用于标准的 Java Reader/Writer ,InputStream/OutputStream 和File 以 ...
- HD1532Drainage Ditches(最大流模板裸题 + 邻接表)
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Mongo报如下类似错误时的修改方法Cannot natively represent the long 1396367483000 on this platform
今天通过php连接mongodb,并读取数据时,其中一个字段为: "createTime":NumberLong("1397524645000"),由于太长,一 ...
- 浅谈PHP与手机APP开发(API接口开发)
了解PHP与API开发 一.先简单回答两个问题: 1.PHP 可以开发客户端? 答:不可以,因为PHP是脚本语言,是负责完成 B/S架构 或 C/S架构 的S部分,即:服务端的开发.(别去纠结 GTK ...
- 输入你的性别,身高及体重,判断你的身材是否标准。(用if......else语句)
Console.WriteLine("请输入你的性别,身高和体重:"); string s = Console.ReadLine(); double h = double.Pars ...
- linux source与 . 命令
source命令用法:source FileName作用:在当前bash环境下读取并执行FileName中的命令.注:该命令通常用命令“.”来替代.如:source .bash_rc 与 . .bas ...
- C++中map的基本操作和使用;
注:本文来自sina live 的博文 Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作!1. map最基本 ...