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 ...
随机推荐
- Sublime Text 3 绝对神器
距第一篇的开箱水文,已经有4个月的时间了,但因为懒,就没有下文了.终于,今天,我觉得写一篇准技术文章了. 忘记了是怎么开始用的ST,应该是在网上看到别人推荐才用到吧,用了有半年了.在windows下是 ...
- Java泛型中E、T、K、V等的含义
Java泛型中的标记符含义: E - Element (在集合中使用,因为集合中存放的是元素) T - Type(Java 类) K - Key(键) V - Value(值) N - Numbe ...
- xml_MathML的基本知识点__这东西要自己实践最好
1 : <mi> 一般的字符串 2: <mo> 操作字符串 <mo> ( </mo> <mo>∑</mo> 3:<mn&g ...
- sql- 别名alias(as)
alias (别名) 在 SQL 上的用处.最常用到的别名有两种: 栏位别名及表格别名. 简单地来说,栏位别名的目的是为了让 SQL 产生的结果易读.在之前的例子中,每当我们有营业额总合时,栏位名都是 ...
- 洛谷P1736 创意吃鱼法
题目描述 回到家中的猫猫把三桶鱼全部转移到了她那长方形大池子中,然后开始思考:到底要以何种方法吃鱼呢(猫猫就是这么可爱,吃鱼也要想好吃法 ^_*).她发现,把大池子视为01矩阵(0表示对应位置无鱼,1 ...
- 关于“wining attitude”
同学转的诺基亚招聘启事上看到这样一则要求:“a real team player with wining attitude”.我的反应先是好奇,后是惊讶!好奇是好奇怎么个wining attitude ...
- LocalDB简介和在VS2012及以上版本的使用
http://www.cnblogs.com/fzrain/p/3812364.html?utm_source=tuicool LocalDB简介和在VS2012及以上版本的使用 之前一不小心把自 ...
- jquery------.mouseover()和.mouseout()的高级效果使用
index.jsp <div style="width:100%;height:40px;background-color:#aaa;position:absolute;"& ...
- linux下svn命令大全
linux下svn命令大全 1.将文件checkout到本地目录 svn checkout path(path是服务器上的目录) 例如:svn checkout svn://192.168.1.1/p ...
- JS 动画基础
获取元素的样式 getStyle函数 function getStyle(element, attr) { if(element.currentStyle) { //针对IE return eleme ...