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 ...
随机推荐
- Bridges
Bridges 题目描述 YYD为了减肥,他来到了瘦海,这是一个巨大的海,海中有n个小岛,小岛之间有m座桥连接,两个小岛之间不会有两座桥,并且从一个小岛可以到另外任意一个小岛.现在YYD想骑单车从小岛 ...
- bzoj 合集 1079 1791 1876 2208 2306
1079 记忆化瞎搞吧,[a][b][c][d][e][l]表示当前有能涂1次的油漆a个,能涂2次的b个….前一个颜色为l,再搞下转移就行了. 1791 基环树上找直径 1876 高精度 2208 看 ...
- vue中如何将时间对象转换成字符串
借鉴element-admin中封装好的方法 import { parseTime } from '@/utils'// 在utils目录下的index.js文件中,方法如下 /** * Parse ...
- [LeetCode] Reverse Bits 位操作
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- LeetCode Weekly Contest 70 A B C D
A. K-th Symbol in Grammar Description On the first row, we write a 0. Now in every subsequent row, w ...
- CC2540介绍
1. 介绍 CC2540是一款2.4GHz Bluetooth® low energy SOC,基于8051 MCU 首先,你需要硬件设备 笔者的开发板为CC2540DK 得到开发板的同时应该还有TI ...
- MongoDB的使用[转]
http://www.cnblogs.com/TankMa/archive/2011/06/08/2074947.html
- python3模拟扑克牌
python3.6环境 import collections from random import choice Card=collections.namedtuple('Card',['rank', ...
- LeetCode OJ-- Generate Parentheses *
https://oj.leetcode.com/problems/generate-parentheses/ 输入n产生n个( ,n个 )组成的所有合法的括号组合. 现在纸上画画,找到规律: 1.每一 ...
- iOS 动画笔记 (二)
有它们俩你就够了! 说明:下面有些概念我说的不怎么详细,网上实在是太多了,说了我觉得也意义不大了!但链接都给大家了,可以自己去看,重点梳理学习写动画的一个过程和一些好的博客! 一:说说这两个三方库,C ...