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

https://leetcode.com/discuss/24282/dp-solution-in-6-lines-with-explanation-f-i-n-g-i-1-g-n-i

如果F(n)表示长度为n的二叉树有多少种结果,则

F(n) = F(0)*F(n-1) + F(1)*F(n-2) + F(2)*F(n-2) + ......+ F(n-1)*F(0)

所以代码如下:

class Solution {
public:
int numTrees(int n) {
if(n <= 0) return 0;
vector<int> nums(n+1,0);
nums[0] = 1;
for(int i = 1; i<=n; i++){
for(int j = 1; j <=i ; j++){
nums[i] += nums[j-1]*nums[i-j];
}
}
return nums[n];
}
};

Unique Binary Search Trees II

Given n, generate all structurally unique BST’s (binary search trees) that store values 1…n.

For example,

Given n = 3, your program should return all 5 unique BST’s shown below.

   1         3     3      2      1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3

同样沿用 I 的思路,利用分治思想

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<TreeNode*> generateTrees(int n) {
if(n <=0) return vector<TreeNode*>();
return generateSubTrees(1,n);
}
vector<TreeNode*>generateSubTrees(int s,int e){
vector<TreeNode*> res;
if(s > e){
res.push_back(NULL);
return res;
}
for(int i = s; i <= e;i++ ){
vector<TreeNode*> left = generateSubTrees(s,i-1);
vector<TreeNode*> right = generateSubTrees(i+1,e); for(auto l : left){
for(auto r : right){
TreeNode* root = new TreeNode(i);
root->left = l;
root->right = r;
res.push_back(root);
}
}
}
return res;
}
};

96. Unique Binary Search Trees(I 和 II)的更多相关文章

  1. leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses

    96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...

  2. [LeetCode] 96. Unique Binary Search Trees 唯一二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  3. 52. leetcode 96. Unique Binary Search Trees

    96. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) tha ...

  4. 96. Unique Binary Search Trees

    题目: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For e ...

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

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

  6. 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 ...

  7. [LeetCode] 96. Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Example ...

  8. [LeetCode] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆

    [Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...

  9. 【LeetCode】96. Unique Binary Search Trees (2 solutions)

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

随机推荐

  1. session添加登录次数限制

    session 中设置了生存期,20分钟,输入密码错误次数保存到session 中,过一段时间自动解除: //登陆的用户名或者密码出错次数 int n = 0; if(logintimes == nu ...

  2. java基础——随机数问题

    /** * 要求:随机打印50个随机(4-10长度)的字符串,要求字符串包含的范围是所有的英文字母包含大小写和数字, * 按照编码顺序排序,每行打印4个,要求首字符对齐 * @author fanyu ...

  3. win10中打开SQL Server 2008 的SQL Server配置管理器方法

    win10找不到SQL Server配置管理器 搜索 SQLServerManager10.msc,或者运行文件:“C:\Windows\SysWOW64\SQLServerManager10.msc ...

  4. JTT808、JTT809、JTT796、JTT794、JTT1077、JTT1078区别与交通部道路运输车辆卫星定位系统部标标准大全下载地址

    部标JT/T808协议.JT/T809协议.JT/T796标准.JT/T794标准的区别,他们是基于不同的通信场景,不同的通信对象,不同的设计目的和目标而制定出来的.首先要知道这些标准的全称是什么意思 ...

  5. oc中将CGRect、CGSize、CGPoint等结构体转换为字符串

    CGRect rect = CGRectMake(160, 230, 200, 200); CGPoint point = CGPointMake(20, 20); CGSize size =  CG ...

  6. 使用objection来模块化开发iOS项目

    转自无网不剩的博客 objection 是一个轻量级的依赖注入框架,受Guice的启发,Google Wallet 也是使用的该项目.「依赖注入」是面向对象编程的一种设计模式,用来减少代码之间的耦合度 ...

  7. Linux下手动备份还原硬盘主引导记录MBR跟硬盘分区表DPT教程

    Linux下手动备份还原硬盘主引导记录MBR跟硬盘分区表DPT教程 二 18 奶牛 Linux, Ubuntu, Windows 1,885 views查看评论 最近奶牛一直在折腾linux下的gru ...

  8. SpringMVC 项目中引用其他 Module 中的方法

    1. 将要引用的Module 引入项目中 2. 在主Module中添加依赖, 3. 被引用的类必须放在 Module 中/src/下的某个package中,否则引用不到(重要)

  9. Python基础学习总结__Day3

    一.集合 1.特性:无序且天生去重,格式为{} 2.作用: (1)去重 (2)关系测试 3.可调用函数(常见对列表操作) (1)取交集:A.intersection(B) (2)取并集:A.union ...

  10. Mysql进入数据库

    进入某个数据库: use db_name; //db_name为数据库名称 mysql> use db_name Database changed