【LeetCode】96. Unique Binary Search Trees (2 solutions)
Unique Binary Search Trees
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
Base case: n==0, n==1时,f(n)==1
递推关系:f(n)=∑f(i)*f(n-i-1)。即以第i个为根节点,左右子树数目相乘。
解法一:递归
class Solution {
public:
int numTrees(int n) {
if(n == )
return ;
else if(n == )
return ;
else
{
int count = ;
for(int i = ; i <= (n-)/; i ++)
{
if(i < n--i)
count += *numTrees(i)*numTrees(n--i);
else
count += numTrees(i)*numTrees(n--i);
}
return count;
}
}
};
解法二:动态规划
class Solution {
public:
int numTrees(int n) {
if(n== || n == )
return ; vector<int> v(n+, );
v[] = ;//n==0
v[] = ;//n==1
for(int i = ; i <= n; i ++)
{//n == i
for(int j = ; j < i; j ++)
{
v[i] += v[j]*v[i--j];
}
}
return v[n];
}
};
【LeetCode】96. Unique Binary Search Trees (2 solutions)的更多相关文章
- 【LeetCode】96. Unique Binary Search Trees 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 记忆化递归 动态规划 卡特兰数 日期 题目地址:ht ...
- 【LeetCode】96 - Unique Binary Search Trees
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 解题报告(Python)
[LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
- 【一天一道LeetCode】#96. Unique Binary Search Trees
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...
- 【LeetCode】95. 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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- LeetCode OJ 96. Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [leetcode tree]96. Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 【LeetCode】98. Validate Binary Search Tree (2 solutions)
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
随机推荐
- nginx+php-fpm 配置和错误总结
<strong>空白页面:</strong>需要这个参数: fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_scrip ...
- FLV视频在IIS6.0下不能播放 处理的方法
FLV视频在IIS6.0下不能播放 Flash视频由于其较高的压缩率和优越的下载速度,前景普遍看好,同时也为Flash课件增色不少.然而,在FLV视频播放中,却有两个头痛的问题 一.FLV视频在 ...
- Internationalization composition diagram
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdHJvdWJsZXNob290ZXI=/font/5a6L5L2T/fontsize/400/fill/I0 ...
- Assignment (HDU 2853 最大权匹配KM)
Assignment Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 关于String 后面跟省略号。。。
今天阅读MonkeyRunner源码的时候发现下面一段: private String shell(String... args) { StringBuilder cmd = new StringBu ...
- (转)基于SQL的EAN13、ENA8条形码校验位生成
USE [DB]GO/****** Object: UserDefinedFunction [dbo].[EAN13] Script Date: 07/04/2017 15:21:51 ******/ ...
- OpenMP 线程同步之临界区
多核/多线程编程中肯定会用到同步互斥操作.除了互斥变量以为,就是临界区. 临界区是指在用一时刻只允许一个线程执行的一段用{...},包围的代码段. 在OpenMP中临界区声明方法如下: #pragma ...
- 通用 CSS 笔记、建议与指导
在参与规模庞大.历时漫长且人手众多的项目时,所有开发者遵守如下规则极为重要: + **保持 CSS 的可维护性** + **保持代码清晰易懂** + **保持代码的可拓展性** 为了实现这一目标,我们 ...
- 刷新SqlServer所有视图元数据的存储过程
摘自: http://www.cnblogs.com/yashen/archive/2004/12/23/81000.html 我们在使用SqlServer时经常遇到这种情况,当修改某个表的结构后,相 ...
- mysql的数据恢复
转载自:http://ourmysql.com/archives/1293 数据库数据被误删除是经常看到的事情,数据的恢复也就自然成为了DBA很重要的一门基本功夫,比较笨拙的办法是拉出历史的备份到另外 ...