题目:

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的更多相关文章

  1. 【二叉查找树】01不同的二叉查找树的个数【Unique Binary Search Trees】

    当数组为1,2,3,4,...,n时,基于以下原则构建的BST树具有唯一性: 以i为根节点的树,其左子树由[1,i-1]构成,其右子树由[i+1, n]构成. 我们假定f(i)为以[1,i]能产生的U ...

  2. 【Unique Binary Search Trees II】cpp

    题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...

  3. 【二叉查找树】02不同的二叉查找树个数II【Unique Binary Search Trees II】

    提到二叉查找树,就得想到二叉查找树的递归定义, 左子树的节点值都小于根节点,右子树的节点值都大于根节点. +++++++++++++++++++++++++++++++++++++++++++++++ ...

  4. 【Validate Binary Search Tree】cpp

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...

  5. 【Recover Binary Search Tree】cpp

    题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...

  6. CF1237E 【Balanced Binary Search Trees】

    首先我们要注意到一个性质:由于根与右子树的根奇偶性相同,那么根的奇偶性与\(N\)相同 然后我们发现对于一个完美树,他的左右两个儿子都是完美树 也就是说,一颗完美树是由两棵完美树拼成的 注意到另一个性 ...

  7. 【一天一道LeetCode】#96. Unique Binary Search Trees

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...

  8. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

  9. 【LeetCode】95. Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

随机推荐

  1. C puzzles详解【6-8题】

    第六题 #include<stdio.h> int main() { ; switch(a) { ': printf("ONE\n"); break; ': print ...

  2. jquery.qrcode.js生成二维码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. VS2008调试快捷键

    F5: 启动调试 Ctrl+F5: 开始执行(不调试) F10: 逐过程(不进入函数单步) F11: 逐语句(进入函数单步) Shift+F11跳出(实用) Ctrl+F10: 运行到光标处 F6: ...

  4. SQL中迁移sql用户及密码脚本

    SQL中迁移sql用户及密码脚本   编写人:CC阿爸 2014-6-20 在日常SQL数据库的操作中,常常需要迁移数据库或重装服务器,这时候,一些之前建立的login账户,必须重新建立,以下可以通过 ...

  5. VS2010在非IE浏览器下调试Silverlight程序

    以Chrome为例: 第一步:在程序中设置断点. 第二步:右键点击web应用程序的起始页(.html或.aspx文件),选择"浏览方式",选中Chrome或其它非IE浏览器,点&q ...

  6. redis的安装过程基本配置及遇到问题的解决

    下载软件包 在centos下如果没有wget先安装 wgetyum -y install wgetwget http://download.redis.io/releases/redis-3.0.0. ...

  7. MySQL: InnoDB 还是 MyISAM?

    MyISAM存储引擎 MyISAM是 默认存储引擎.它基于更老的ISAM代码,但有很多有用的扩展.MyISAM存储引擎的一些特征:·      所有数据值先存储低字节.这使得数据机和操作系统分离.二进 ...

  8. jquery 点击页面其他地方实现隐藏菜单功能

    1.给页面文档添加一个点击事件函数,在函数内实现隐藏菜单功能. $('html').click(function(){//Hide the menus if visible});//用$(docume ...

  9. C#高级功能(一)Lambda 表达式

    Lambda 表达式是一种可用于创建委托或表达式目录树类型的匿名函数. 通过使用 lambda 表达式,可以写入可作为参数传递或作为函数调用值返回的本地函数. Lambda 表达式对于编写 LINQ ...

  10. 在linux下安装Mongodb

    1.到官网下载源码:mongodb-linux-x86_64-rhel55-3.2.4.gz 2.安装 创建用户组.用户.目录 [root@hadoop1 ~]# groupadd mongodb [ ...