【Unique Binary Search Trees】cpp
题目:
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
代码:
class Solution {
public:
int numTrees(int n) {
vector<int> dp(n+,);
dp[] = ;
dp[] = ;
for ( size_t i = ; i < n+; ++i ){
for ( size_t j = ; j < i; ++j ){
dp[i] += dp[j] * dp[i-j-];
}
}
return dp[n];
}
};
tips:
1. ‘左子树可能数*右子树可能数’为所有以元素i为根节点的BST个数。
2. 如果总个数是n,则把根节点为1~n的情况都累加一遍,就是不重复的BST个数(由于要用到之前的计算结果,因此一维dp很合适)
===========================================
第二次过这道题,这题其实放DP里更好一些。
注意初始化的时候,一般都初始化为0。dp[0] dp[1]特殊处理。
class Solution {
public:
int numTrees(int n) {
vector<int> dp(n+,);
dp[] = ;
dp[] = ;
for ( int i=; i<=n; ++i )
{
for ( int j=; j<i; ++j )
{
dp[i] += dp[j] * dp[i-j-];
}
}
return dp[n];
}
};
class Solution {
public:
int numTrees(int n) {
vector<int> dp(n+,);
dp[] = ;
dp[] = ;
for ( int i=; i<=n; ++i )
{
for ( int j=; j<i; ++j )
{
dp[i] += dp[j] * dp[i-j-];
}
}
return dp[n];
}
};
【Unique Binary Search Trees】cpp的更多相关文章
- 【二叉查找树】01不同的二叉查找树的个数【Unique Binary Search Trees】
当数组为1,2,3,4,...,n时,基于以下原则构建的BST树具有唯一性: 以i为根节点的树,其左子树由[1,i-1]构成,其右子树由[i+1, n]构成. 我们假定f(i)为以[1,i]能产生的U ...
- 【Unique Binary Search Trees II】cpp
题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...
- 【二叉查找树】02不同的二叉查找树个数II【Unique Binary Search Trees II】
提到二叉查找树,就得想到二叉查找树的递归定义, 左子树的节点值都小于根节点,右子树的节点值都大于根节点. +++++++++++++++++++++++++++++++++++++++++++++++ ...
- 【Validate Binary Search Tree】cpp
题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...
- 【Recover Binary Search Tree】cpp
题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...
- CF1237E 【Balanced Binary Search Trees】
首先我们要注意到一个性质:由于根与右子树的根奇偶性相同,那么根的奇偶性与\(N\)相同 然后我们发现对于一个完美树,他的左右两个儿子都是完美树 也就是说,一颗完美树是由两棵完美树拼成的 注意到另一个性 ...
- 【一天一道LeetCode】#96. Unique Binary Search Trees
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...
- 【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) ...
随机推荐
- s3c6410_uboot中的代码重定位(nand->sdram)
本文仅探讨s3c6410从nand flash启动u-boot时的代码重定位过程 参考: 1)<USER'S MANUAL-S3C6410X>第二章 MEMORY MAP 第八章 NAND ...
- ios网络:应用一个请求的7个步骤
Splitting big tasks into small tasks is often one of the best ways to solve a problem. Thus, in the ...
- Visual Studio 2010 更新NuGet Package Manager出错解决办法
在Visual Studio 2010的扩展管理器中发现NuGet Package Manger有最新版本更新提示,选择更新安装提示以下错误信息: 2013/4/25 1:11:48 - Micros ...
- c# 如何获取项目的根目录
c# 如何获取项目的根目录 编写程序的时候,经常需要用的项目根目录.自己总结如下 1.取得控制台应用程序的根目录方法 方法1.Environment.CurrentDirectory 取得或设 ...
- [原]打造Python开发环境之Python环境
人生苦短,我用Python 一.升级到python2.7 开发环境的系统是centos 6.0, 默认的python版本是2.6.6, 由于线上环境是python2.7,为了防止版本差异产生的问题,所 ...
- BGP学习笔记
源自红茶三杯: BGP应用于大规模网络或运营商,用作在AS间传递路由信息 使用BGP的三大理由 1. 大量路由需要承载, IGP只能容纳千条,而BGP可以容纳上万(应该是IGP结合BGP使用?) 2. ...
- spark性能调优:资源优化
在开发完Spark作业之后,就该为作业配置合适的资源了.Spark的资源参数,基本都可以在spark-submit命令中作为参数设置.很多Spark初学者,通常不知道该设置哪些必要的参数,以及如何设置 ...
- tp框架集成支付宝,中转页变成gbk编码
tp框架中集成支付宝的功能,将支付宝的demo例子存在到下图位置\Extend\Vendor\Alipay 生成支付订单 /** * 支付订单 */ public function pay() { h ...
- jQuery打造用户注册时获取焦点文本框出现提示jquery表单特效
jQuery打造用户注册时获取焦点文本框出现提示效果的jquery表单特效 当获取焦点时,会分别的弹出相应的信息提示框,失去焦点就会隐藏提示信息. 效果兼容性很高,适用浏览器:IE6.IE7.IE8. ...
- WordPress 主题开发 - (四) 创建WordPress的主题HTML结构 待翻译
Now we're starting to get into the real meat of WordPress Theme development: coding the HTML structu ...