Unique Binary Search Trees,Unique Binary Search Trees II
Unique Binary Search Trees
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> map;
map.push_back();
for (int i = ; i <= n; ++i) {
int t = ;
for (int j = ; j < i; ++j)
t += map[j] * map[i-j-];
map.push_back(t);
}
return map.back();
}
};
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
confused what "{1,#,2,3}"
means? > read more on how binary tree is serialized on OJ.
/**
* 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 start,int end){
vector<TreeNode*> res;
if(end < start) {
res.push_back(NULL);
return res;
}
for(int rootValue = start; rootValue<=end; ++rootValue){
vector<TreeNode*> left = generateTrees(start,rootValue-);
vector<TreeNode*> right = generateTrees(rootValue+,end);
for(int i=;i<left.size();++i){
for(int j=;j<right.size();++j){
TreeNode *root = new TreeNode(rootValue);
root->left = left[i];
root->right = right[j];
res.push_back(root);
}
}
}
return res;
}
vector<TreeNode*> generateTrees(int n) {
return n== ? vector<TreeNode*>():generateTrees(,n);
}
};
Unique Binary Search Trees,Unique Binary Search Trees II的更多相关文章
- 将百分制转换为5分制的算法 Binary Search Tree ordered binary tree sorted binary tree Huffman Tree
1.二叉搜索树:去一个陌生的城市问路到目的地: for each node, all elements in its left subtree are less-or-equal to the nod ...
- 04-树7. Search in a Binary Search Tree (25)
04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 ...
- pat04-树7. Search in a Binary Search Tree (25)
04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 ...
- [Algorithms] Refactor a Linear Search into a Binary Search with JavaScript
Binary search is an algorithm that accepts a sorted list and returns a search element from the list. ...
- 【Leetcode_easy】700. Search in a Binary Search Tree
problem 700. Search in a Binary Search Tree 参考1. Leetcode_easy_700. Search in a Binary Search Tree; ...
- 41. Unique Binary Search Trees && Unique Binary Search Trees II
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
- Leetcode:Unique Binary Search Trees & Unique Binary Search Trees II
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
- LeetCode之“动态规划”:Unique Binary Search Trees && Unique Binary Search Trees II
1. Unique Binary Search Trees 题目链接 题目要求: Given n, how many structurally unique BST's (binary search ...
- Unique Binary Search Trees,Unique Binary Search Trees2 生成二叉排序树
Unique Binary Search Trees:求生成二叉排序树的个数. Given n, how many structurally unique BST's (binary search t ...
随机推荐
- checkbox 实现单选效果(html)
note:在html <input> 标签类中的checkbox实现单选效果. 在最近的开发项目中,客户要求使用小方格子实现“单选”功能,显然圆点的radio被out了,只能选择chckb ...
- [Head First Python]6. summary
1- 字典-内置数据结构,数据值与键值关联 键-字典中查找部分 值-字典中数据部分 使用dict()工厂函数或者只用{}可以创建一个空字典 >>> list = {} >> ...
- laravel 心得
1.安装 使用composer安装laravel ,切换到你想要放置该网站的目录下,运行命令: composer create-project laravel/larevel project 4.1 ...
- WordPress插件制作教程(一): 如何创建一个插件
上一篇还是按照之前的教程流程,写了一篇WordPress插件制作教程概述,从这一篇开始就为大家具体讲解WordPress插件制作的内容.这一篇主要说一下插件的创建方法. 相信大家都知道插件的安装文件在 ...
- 高可用集群(HA)之DRBD原理和基础配置
目录 1.工作原理图 2.用户空间工具 3.工作模式 4.实现主备故障自动切换 5.所需软件 6.配置文件 7.详细配置 1.配置通用属性信息 2.定义一个资源 3.初始化资源 ...
- wordpress教程之the_author_meta()显示用户的信息
描述 模板标签函数the_author_meta可以显示用户数据.如果该函数在文章主循环(Loop)中,则不必指定作者的ID值,标签所显示的就是当前文章作者的内容.如果在主循环(Loop)外,则需要指 ...
- HDU 3634 City Planning (离散化)
City Planning Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- ObjectOutputStream 追加写入读取错误 - 自己的实现方案
本篇博客灵感来自http://blog.csdn.net/chenssy/article/details/13170015 问题描述.问题出现的原因.尝试解决办法,请参见鄙人上一编博客. 上一编文章解 ...
- 【BZOJ1031】[JSOI2007]字符加密Cipher 后缀数组
[BZOJ1031][JSOI2007]字符加密Cipher Description 喜欢钻研问题的JS同学,最近又迷上了对加密方法的思考.一天,他突然想出了一种他认为是终极的加密办法 :把需要加密的 ...
- Python递归函数与斐波那契数列
定义:在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. 阶乘实例 n = int(input(">>:")) def f(n): s ...