Unique Binary Search Trees(dp)
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
BST树的定义:根节点左边所有节点的值小于根节点值,右边所有节点的值大于根节点的值,然后其左右子树又是BST 思路:例如序列:1,2,3,4,5
第一个数字的左边有0个数字,右边4个数字 dp[0]*dp[4]
第二个数字的左边有1个数字,右边3个数字 dp[1]*dp[3]
第三个数字的左边有2个数字,右边2个数字 dp[2]*dp[2]
第四个数字的左边有3个数字,右边1个数字 dp[3]*dp[1]
第五个数字的左边有4个数字,右边0个数字 dp[4]*dp[0]
所以我们要求的dp[5]就等于上面加起来的和,这里我们得假设dp[0]=1;
其实就是每个数字来当根节点,具体如下图所示

代码:
class Solution{
public:
int numTrees(int n) {
if(n<=) return n;
vector<int> dp(n+,);
dp[]=;
for(int i=;i<=n;++i){
int temp=;
for (int j=;j<=i;++j)
{
temp+=dp[j-]*dp[i-j];
}
dp[i]=temp;
}
return dp[n];
}
};
Unique Binary Search Trees(dp)的更多相关文章
- LeetCode Unique Binary Search Trees (DP)
题意: 一棵BST有n个节点,每个节点的key刚好为1-n.问此树有多少种不同形态? 思路: 提示是动态规划. 考虑一颗有n个节点的BST和有n-1个节点的BST.从n-1到n只是增加了一个点n,那么 ...
- 96. Unique Binary Search Trees(I 和 II)
Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For example ...
- 96. Unique Binary Search Trees (Tree; DP)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- LeetCode OJ:Unique Binary Search Trees(唯一二叉搜索树)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 40.Unique Binary Search Trees(不同的二叉搜索树)
Level: Medium 题目描述: Given n, how many structurally unique BST's (binary search trees) that store v ...
- 【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
题目 Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For exam ...
- 【LeetCode-面试算法经典-Java实现】【096-Unique Binary Search Trees(唯一二叉搜索树)】
[096-Unique Binary Search Trees(唯一二叉搜索树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given n, how many s ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
随机推荐
- ubuntu下安装redis扩展
采用源码编译安装 下载 下载redis源码包到本地一个临时目录 git clone https://github.com/phpredis/phpredis.git 移动文件到合适目录 mv phpr ...
- 图解GitHub
转自:http://marklodato.github.io/visual-git-guide/index-zh-cn.html 个人觉得这一篇比一些入门教程更值得看,图解很详细到位,很容易理解其工作 ...
- 大型Java Web项目的架构和部署问题
一位ID是jackson1225的网友在javaeye询问了一个大型Web系统的架构和部署选型问题,希望能提高现有的基于Java的Web应用的服务能力.由于架构模式和部署调优一直是Java社区的热门话 ...
- Sql Server数据库对象访问权限控制
以下内容主要针对database层面的数据访问权限(比如select, insert, update, delete, execute…) 1.直接给user权限GRANT EXECUTE TO [u ...
- (转)让Spring自动扫描和管理Bean
http://blog.csdn.net/yerenyuan_pku/article/details/52861403 前面的例子我们都是使用XML的bean定义来配置组件.在一个稍大的项目中,通常会 ...
- js 输出某年某月某日的天数/判断闰年
console.log(getDays(2017,12,12)); function getDays(year,month,day){ var arr = [31,28,31,30,31,30,31, ...
- No-3.Linux 终端命令格式
Linux 终端命令格式 01. 终端命令格式 command [-options] [parameter] 说明: command:命令名,相应功能的英文单词或单词的缩写 [-options]:选项 ...
- ubuntu install zabbix
ubuntu install zabbix reference1 reference2 some ERRORS raise during install process, may it help. z ...
- 框模型中设置内容区域元素占地尺寸box-sizing属性
盒子模型有两种 一种是 内容盒子模型(content-box)一种是边框盒子模型(border-box). content-box:设置的尺寸,只设置内容区域, 左外边距+左边框+左内边距+内容区域宽 ...
- Spring框架针对dao层的jdbcTemplate操作之jdbc数据库连接原始操作方法 所需安装包下载
crud指数据库或者持久层的基本操作,包括 增加(Create).读取查询(Retrieve 取回).更新(Update)和删除(Delete) Spring不仅对JDBC进行了封装,也对Hibern ...