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 ...
随机推荐
- java替换富文本标签等
(转自:http://www.cnblogs.com/1246447850qqcom/p/5439366.html) package q;import java.util.regex.Matcher; ...
- input[type="radio"]自定义样式
input为radio时,虽然会有默认选中的样式,但是并不符合大多数项目的需求,我们的目标是可以随心所欲自定义它的样式.怎么做呢?其实很简单,只要抓住3点.分别是1.label 2.隐藏自带样式 3. ...
- Transformer解析与tensorflow代码解读
本文是针对谷歌Transformer模型的解读,根据我自己的理解顺序记录的. 另外,针对Kyubyong实现的tensorflow代码进行解读,代码地址https://github.com/Kyuby ...
- NodeJS学习(1)--- 安装配置介绍
Node.js 安装配置 本章节我们将向大家介绍在window和Linux上安装Node.js的方法. 本安装教程以Node.js v6.10.1 LTS(长期支持版本)版本为例. Node.js安装 ...
- 转::iOS 仿淘宝,上拉进入详情页面
今天做的主要是一个模仿淘宝,上拉进入商品详情的功能,主要是通过 tableView 与 webView 一起来实现的,当然也可根据自己的需要把 webView 替换成你想要的 // // ViewCo ...
- CodeForces 605 E. Intergalaxy Trips
E. Intergalaxy Trips time limit per test:2 seconds memory limit per test:256 megabytes input:standar ...
- ZOJ1608 Two Circles and a Rectangle
Time Limit: 2 Seconds Memory Limit: 65536 KB Give you two circles and a rectangle, your task is ...
- no update
cd /home/.gnupg/ mv gpg.conf gpgbake.conf pacman -S archlinux-keyring && pacman -Syu 如果还是不行, ...
- layui 的Tab选项卡
http://www.layui.com/doc/element/tab.html <#--start--> <div class="layui-tab layui-tab ...
- Android,一条线串联实心圆布局
最近遇到一个简单的布局,不是listview的形式.就只是单纯的下图这种: 此界面布局代码: <?xml version="1.0" encoding="utf-8 ...