Unique Binary Search Trees II leetcode java
题目:
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.
For example,
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 题解:
这道题比1难的就是不是返回个数,而是返回所有结果。
引用code ganker(http://codeganker.blogspot.com/2014/04/unique-binary-search-trees-ii-leetcode.html)的讲解:
”这道题是求解所有可行的二叉查找树,从Unique Binary Search Trees中我们已经知道,可行的二叉查找树的数量是相应的卡特兰数,不是一个多项式时间的数量级,所以我们要求解所有的树,自然是不能多项式时间内完成的了。算法上还是用求解NP问题的方法来求解,也就是N-Queens中
介绍的在循环中调用递归函数求解子问题。思路是每次一次选取一个结点为根,然后递归求解左右子树的所有结果,最后根据左右子树的返回的所有子树,依次选取
然后接上(每个左边的子树跟所有右边的子树匹配,而每个右边的子树也要跟所有的左边子树匹配,总共有左右子树数量的乘积种情况),构造好之后作为当前树的
结果返回。
“
这道题的解题依据依然是:
当数组为 1,2,3,4,.. i,.. n时,基于以下原则的BST建树具有唯一性:
以i为根节点的树,其左子树由[1, i-1]构成, 其右子树由[i+1, n]构成。
代码如下:
1 public ArrayList<TreeNode> generateTrees(int n) {
2 return generateTrees(1, n);//从1作为root开始,到n作为root结束
3 }
4
5 private ArrayList<TreeNode> generateTrees(int left, int right){
6 ArrayList<TreeNode> res = new ArrayList<TreeNode>();
7 if (left > right){
8 res.add(null);
9 return res;
}
for (int i = left; i <= right; i++){
ArrayList<TreeNode> lefts = generateTrees(left, i-1);//以i作为根节点,左子树由[1,i-1]构成
ArrayList<TreeNode> rights = generateTrees(i+1, right);//右子树由[i+1, n]构成
for (int j = 0; j < lefts.size(); j++){
for (int k = 0; k < rights.size(); k++){
TreeNode root = new TreeNode(i);
root.left = lefts.get(j);
root.right = rights.get(k);
res.add(root);//存储所有可能行
}
}
}
return res;
}
Unique Binary Search Trees II leetcode java的更多相关文章
- Unique Binary Search Trees II - LeetCode
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(给定一个数字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 解题报告(Python)
[LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
- 【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) ...
- LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II
1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...
- 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]* ...
- 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 ...
随机推荐
- python基础之return,参数
函数的返回值 1.什么是返回值: 返回值是一个函数的处理结果 2.为什么要有返回值 如果需要在程序中拿到函数的处理结果,做进一步的处理,则需要函数必须有返回值 3.函数返回值的应用: 函数的返回值用r ...
- BZOJ2973 : 石头游戏
考虑到$lcm(1,2,3,4,5,6)=60$,所以操作序列每60秒一个循环. 将操作表示成转移矩阵的形式,预处理出前60秒的转移矩阵以及它们的乘积$B$. 那么t秒的转移矩阵为前$t\bmod 6 ...
- Django-Filter源码解析一
Django Filter源码解析 最近在看Django-FIlter项目的源码,学习一下别人的开发思想: 整体介绍 首先,我从其中一个测试用例作为入口,开始了debug之路,一点一点的断点,分析它的 ...
- URAL 1963 Kite 计算几何
Kite 题目连接: http://acm.hust.edu.cn/vjudge/contest/123332#problem/C Description Vova bought a kite con ...
- FireDAC 下的 Sqlite [8] - 自定义函数
Sqlite 本身没有这个功能, FireDAC 通过 TFDSQLiteFunction 增加了该功能; 尽管通过某些 SQL 语句或通过视图也可以达到类似效果, 但函数会更灵活些. 本例先建了一个 ...
- Go 面试题(附答案解析)
1.写出下面代码输出内容 package main import ( "fmt" ) func main() { defer_call() } func defer_call() ...
- self.xxx = nil 可以等效于[_xxx release] _xxx= nil 么
如果属性是copy.retain的话是等价的.如下: - (void)setXXX:(NSString*)axx { if (_xxx != axx) { [_xxx release]; _xxx = ...
- itunes connect 沙盒帐号地区的问题导致无法进行充值
项目代理给台湾发行,版本由项目提交appstore,台湾合作伙伴会进行测试.这里遇到一个问题,就是沙盒帐号测试的时候死活提示充值失败,不走正常的充值流程. 后来我分析了原因,主要是由于帐号的App S ...
- cocos2d-x 保持屏幕点亮及自动变灰
很早之前遇到的问题,现在记录一下.有一家Android渠道(抱歉,时间太长了已经记不大清楚是哪一家了 oppo/联想/酷派?)在我们提交新版本时拒绝了,理由是:手机背光状态下,屏幕不会自动变灰. 这里 ...
- ExtJS 4.2 教程-04:数据模型
转载自起飞网,原文地址:http://www.qeefee.com/extjs-course-4-data-model ExtJS 4.2 教程-01:Hello ExtJS ExtJS 4.2 教程 ...