LeetCode-96. Unique Binary Search Trees
Description:
Given n, how many structurally unique BST's (binary search trees) that store values 1...n?
For example,
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
问题大意:给一个整数n,计算出节点数为n的二叉查找树的种类。
这个题目最简单的思路应该是递归构造二叉查找树,让每个节点都成为根节点。这样时间复杂度较高。还有一种动态规划法。描述如下:
假如整个树有 n 个节点,根节点为 1 个节点,两个子树平分剩下的 n-1 个节点。
假设我们已经知道节点数量为 x 的二叉树有dp[x]种不同的形态。
则一颗二叉树左节点节点数量为 k 时,其形态数为dp[k] * dp[n - 1 - k]。
而对于一颗 n 个节点的二叉树,其两个子树分配节点的方案有 n-1 种:
(0, n-1), (1, n-2), ..., (n-1, 0)
因此我们可以得到对于 n 个节点的二叉树,其形态有:
Sigma(dp[i] * dp[n-1-i]) | i = 0 .. n-1
并且可以发现,dp数组有递推关系,我们可以使用递推或是记忆化搜索来实现。
边界条件为dp[0] = 1。
以上分析来源这里
Java代码:
public class Solution {
int[] rc;
public int numTrees(int n) {
rc = new int[n+1];
Arrays.fill(rc, 0);
return dp(n);
}
public int dp(int nodes) {
if(nodes <= 1)
return 1;
if(rc[nodes] != 0)
return rc[nodes];
int numTrees = 0;
for(int i=0; i<nodes; i++) {
numTrees += dp(i) * dp(nodes-i-1);
}
rc[nodes] = numTrees;
return numTrees;
}
}
LeetCode-96. Unique Binary Search Trees的更多相关文章
- [LeetCode] 96. Unique Binary Search Trees 唯一二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 52. leetcode 96. Unique Binary Search Trees
96. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) tha ...
- 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]* ...
- [LeetCode] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆
[Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...
- [LeetCode] 96. Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Example ...
- leetcode 96 Unique Binary Search Trees ----- java
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- Java [Leetcode 96]Unique Binary Search Trees
题目描述: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For ...
- [leetcode]96. Unique Binary Search Trees给定节点形成不同BST的个数
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Input: ...
- [leetcode] 96 Unique Binary Search Trees (Medium)
原题 字母题 思路: 一开始妹有一点思路,去查了二叉查找树,发现有个叫做卡特兰数的东西. 1.求可行的二叉查找树的数量,只要满足中序遍历有序. 2.以一个结点为根的可行二叉树数量就是左右子树可行二叉树 ...
- [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 ...
随机推荐
- cmd for 循环拷贝文件
这几天忙活部署测试环境, 中途需要拷贝 文件, 直接贴code吧: ::/定义原路径 set source=seventrat_test_backend,seventrat_test_frontend ...
- 八卦一下黄晓明和Angelababy的电话号码
最新一期20150605的<奔跑吧兄弟>真是太搞笑了,邓超被大家整的... 但这一期有个细节引起了我的注意,就是Angelababy在现场打电话给黄晓明,而拨键声音十分清晰.一些拥有“绝对 ...
- win7下安装ubuntu13.04
http://teliute.org/linux/Ubsetup/jichu1/jichu1.html xp下安装 http://flysqrlboy.blog.51cto.com/5027074/8 ...
- Apache Error: Invalid command ‘Allow’, perhaps misspelled or defined by a module not included in the server configuration
在一个Window Server 2008R2系统上使用Apache架设了一个PHP的网站项目 在配置Apache的过程中出现了以下问题 根据上面的提示说是没有相应的权限,那就在虚拟主机里进行了配 ...
- Scala 深入浅出实战经典 第55讲:Scala中Infix Type实战详解
王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-64讲)完整视频.PPT.代码下载: 百度云盘:http://pan.baidu.com/s/1c0noOt ...
- CSS3学习笔记——伪类hover
最近看到一篇文章:“Transition.Transform和Animation使用简介及应用展示” ,想看看里面 “不同缓动类效果demo”例子的效果,发现了一个问题如下: .Trans_Bo ...
- TextBox 文本框水印文字
#region TextBox 文本框水印文字 /// <summary> /// 基于.NET 2.0的TextBox工具类 /// </summary> public st ...
- JS实现移动端图片延迟加载
图片延迟加载常见的有,jquery.lazyload.js,原生JS实现的echo.js.但是都是必须给图片设置宽高. 因为项目是移动端,而且无法在加载前知道图片的宽高,所以,只好自己写了一个. 既然 ...
- Android中GridView使用总结
1.http://blog.csdn.net/hellogv/article/details/4567095 基础篇,GridView最基本的用法 2.http://my.eoe.cn/cainia ...
- OceanBase RPC机制简要说明
http://note.youdao.com/share/?id=d2163a7ba8ec1ec58e64683a961c5121&type=note RPC是OB一个比较重要的各个组件间进行 ...