Unique Binary Search Trees I&II——给定n有多少种BST可能、DP
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> v(n+,);
if(n<) return n;
v[]=;
for(int i=;i<=n;i++){
if(i<){
v[i]=i;
continue;
}
for(int j=;j<=i;j++){
v[i]+=v[j-]*v[i-j];
}
}
return v[n];
}
};
II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.
递归获取left——right的所有可能树,并存在vector中,以供上一层取值
/**
* Definition for binary tree
* 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) {
return sol(,n);
}
vector<TreeNode *> sol(int left, int right){
vector<TreeNode *> res; //保障res只会输出当前获取的树
if(left>right){
res.push_back(NULL); //否则返回后取值时会得到野指针
return res;
} for(int i=left;i<=right;i++){
vector<TreeNode *> leftlist=sol(left,i-);
vector<TreeNode *> rightlist=sol(i+,right);
for(int j=;j<leftlist.size();j++){
for(int k=;k<rightlist.size();k++){
TreeNode *root=new TreeNode(i);
root->left=leftlist[j];
root->right=rightlist[k];
res.push_back(root);
}
}
}
return res;
} };
Unique Binary Search Trees I&II——给定n有多少种BST可能、DP的更多相关文章
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
- Unique Binary Search Trees I & II
Given n, how many structurally unique BSTs (binary search trees) that store values 1...n? Example Gi ...
- leetcode -day28 Unique Binary Search Trees I II
1. Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search t ...
- Unique Binary Search Trees I&&II(II思路很棒)——动态规划(II没理解)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For exa ...
- [leetcode]95. Unique Binary Search Trees II给定节点形成不同BST的集合
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- 【LeetCode】95. Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 【leetcode】Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 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 ...
随机推荐
- 【bzoj3083】遥远的国度 树链剖分+线段树
题目描述 描述zcwwzdjn在追杀十分sb的zhx,而zhx逃入了一个遥远的国度.当zcwwzdjn准备进入遥远的国度继续追杀时,守护神RapiD阻拦了zcwwzdjn的去路,他需要zcwwzdjn ...
- [LOJ#2324]「清华集训 2017」小Y和二叉树
[LOJ#2324]「清华集训 2017」小Y和二叉树 试题描述 小Y是一个心灵手巧的OIer,她有许多二叉树模型. 小Y的二叉树模型中,每个结点都具有一个编号,小Y把她最喜欢的一个二叉树模型挂在了墙 ...
- BZOJ3999 [TJOI2015]旅游 【树剖 + 线段树】
题目 为了提高智商,ZJY准备去往一个新世界去旅游.这个世界的城市布局像一棵树.每两座城市之间只有一条路径可 以互达.每座城市都有一种宝石,有一定的价格.ZJY为了赚取最高利益,她会选择从A城市买入再 ...
- Optimal Marks(optimal)
Optimal Marks(optimal) 题目描述 定义无向图边的值为这条边连接的两个点的点权异或值. 定义无向图的值为无向图中所有边的值的和. 给定nn个点mm条边构成的图.其中有些点的权值是给 ...
- zookeeper与Kafka集群搭建及python代码测试
Kafka初识 1.Kafka使用背景 在我们大量使用分布式数据库.分布式计算集群的时候,是否会遇到这样的一些问题: 我们想分析下用户行为(pageviews),以便我们设计出更好的广告位 我想对用户 ...
- [暑假集训--数论]poj2909 Goldbach's Conjecture
For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 ...
- 使用filter: blur() 的时候解决图片周围泛白和容器外范围变模糊的问题
类似于这种,这个时候出现了周围变模糊,并且边缘泛白的情况 周围模糊这个问题很好解决,给父容器加overflow:hidden:就可以了 效果如上,至于周围泛白的问题就需要动点脑筋了,给目标添加 tra ...
- java网络编程学习笔记(三):ServerSocket详解
1.ServerSocket的构造方法 ServerSocket(); ServerSocket(int port); ServerSocket(int port,int backlog); Serv ...
- 转 c++多线程编程
c++多线程编程 一直对多线程编程这一块很陌生,决定花一点时间整理一下. os:ubuntu 10.04 c++ 1.最基础,进程同时创建5个线程,各自调用同一个函数 #include <io ...
- RSTP介绍
1. 介绍 RSTP(Rapid Spanning Tree Protocol),快速生成树协议,标准为802.1w(已合入802.1D-2004)RSTP是对STP技术的修改和补充,最大特点就是快速 ...