Unique Binary Search Trees 解答
Question
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
Solution
Key to the problem here is to separate this problem into two smaller problems, unique BST's number of right child tree and left child tree.
The pseudocode is like:
for (int i = 1; i <= n; i++) {
result = result + nums[1 ~ i - 1] + nums[i + 1 ~ n]
}
public class Solution {
public int numTrees(int n) {
int[] dp = new int[n + 1];
dp[0] = 1;
for(int j = 1; j <= n; j++) {
dp[j] = 0;
for(int i = 1; i <= j; i++)
dp[j] = dp[i - 1] * dp[j - i] + dp[j];
}
return dp[n];
}
}
Unique Binary Search Trees 解答的更多相关文章
- Unique Binary Search Trees II 解答
Question Given n, generate all structurally unique BST's (binary search trees) that store values 1.. ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] 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
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 【leetcode】Unique Binary Search Trees
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) ...
- 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) ...
- LeetCode - Unique Binary Search Trees II
题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...
随机推荐
- windows media player 中播放pls的方法
windows media player目前只能播放 wpl 和 asm格式的列表文件.而linux下mplayer和vlc支持的pls,很遗憾没法支持. 不过,老外写了个“open pls in w ...
- dispatch_group_async
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. dispat ...
- IOS基于新浪微博开放平台微博APP
1.基于新浪微博开放平台APP源码 2.gitHub源代码下载地址 https://github.com/whzhaochao/SinaWeiBoOpen 3.用到的第三放开源库 3.1 RTLab ...
- MFC获取当前时间
获取按钮消息响应函数: void CTest17GetTimeDlg::OnGetTime() { // TODO: 在此添加控件通知处理程序代码 //UpdateData(true); CTime ...
- ThinkPHP整合百度Ueditor图文教程
ThinkPHP整合百度Ueditor图文教程 ThinkPHP整合百度Ueditor,基于黄永成老师的视频说明的申明:最好大家都能写绝对路径的都写好绝对路径比如:window.UEDITOR_HOM ...
- css解决文字垂直居中
参考链接: http://www.cnblogs.com/lufy/archive/2012/09/12/2681972.html http://zhidao.baidu.com/question ...
- Android SQLite的使用2(非原创)
1.数据库的增.删.改.查:execSQL方法 public void insertAction() {//添加信息 db.execSQL("insert into Emp(name,sal ...
- SQL server根据值搜表名和字段
DECLARE @what varchar(800) SET @what='lll' --要搜索的字符串 DECLARE @sql varchar(8000) DECLARE TableCursor ...
- SQL Server 数据类型 Decimal介绍
为SQL Server 数据类型,属于浮点数类型.存储数据范围是: -1038~1038-1 的固定精度和小数位的数字.一个decimal类型的数据占用了2~17个字节.decimal数据类型在SQL ...
- 图的广度、深度优先遍历 C语言
以下是老师作为数据结构课的作业的要求,没有什么实际用处和可以探讨和总结的的地方,所以简单代码直接展示. 宽度优先遍历: #include<cstdio> #include<iostr ...