【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) ...
随机推荐
- Eclipse 中打不开android sdk managerf
今天配置android sdk 的时候,出现了android sdk 打不开的情况.无论直接点击 sdk manager.exe 还是从eclipse启动,都不起作用,双重启(重启eclipse和ad ...
- 泛型集合转换为DataTable
在做项目中,遇到了将集合转换为DataTable的使用,在网上看了资料,在这里记录下来,分享. using System; using System.Collections.Generic; usin ...
- 【转】Javascript 严格模式详解
ref: http://www.ruanyifeng.com/blog/2013/01/javascript_strict_mode.html 一.概述 除了正常运行模式,ECMAscript 5添加 ...
- firefox 中碰到的一个小坑
情况描述: 在一个处于正常文档流的div中,里面有一部分文字,还有个有浮动的块, 上代码 HTML: <div class="container"> this is ...
- (转)Android L Ripple的使用
声明:Demo并不是有本人所写,本人只是总结在这里 工程源码: RippleDemo.zip ---------------------------------------------------- ...
- 通过HttpClient方式连接网络
xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t ...
- js实现touch移动触屏滑动事件
在开始描述touch事件之前,需要先描述一下多触式系统中特有的touch对象(android和iOS乃至nokia最新的meego系统都模拟了类 似的对象).这个对象封装一次屏幕触摸,一般来自于手指. ...
- mongodb 3.2存储目录结构说明
[root@hadoop1 mongodb]# tree ./data ./data |-- WiredTiger | |-- WiredTiger.lock | |-- WiredTiger.tur ...
- python学习第二天第一部分
备注:写程序不能写重复性的代码 学习内容:数据类型.for循环.while循环.字符编码.文件处理 一.for循环 1.简单的for循环 for i in range(10): # 此处意思为:循环r ...
- C# 编译JS -Evaluator
忘记哪里转过来的,自己mark一下 //// <summary> /// 动态求值 /// </summary> public class Evaluator { /// &l ...