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 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
二叉排序树(二叉排序树(Binary Sort Tree)又称二叉查找树(Binary Search Tree),亦称二叉搜索树。)或者是一棵空树,或者是具有下列性质的二叉树:
class Solution {
public:
int numTrees(int n) {
vector<int> num;
if (n<1)
return 0;
if(n==1)
return 1;
if(n==2)
return 2;
num.push_back(1);
for(int i=1;i<3;i++)
num.push_back(i);
for(int i=3;i<=n;i++)
{
num.push_back(0);
for(int j=0;j<i;j++)
num[i]+=num[j]*num[i-j-1];
}
return num[n];
}
};
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
对于II,网上没看到用动态规划的,都是暴力解法——枚举。用递归完成,还是得感慨一下,我的代码思维好乱啊,不像是个理科生啊!!!!!
/**
* 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 *> generateRes(int left,int right)
{
vector<TreeNode *> res;
if(left>right)
{
res.push_back(NULL);
return res;
}
for(int i=left;i<=right;i++)
{
vector<TreeNode *>leftpart=generateRes(left,i-1);
vector<TreeNode *>rightpart=generateRes(i+1,right);
for(int j=0;j<leftpart.size();j++)
for(int k=0;k<rightpart.size();k++)
{
TreeNode* node=new TreeNode(i);
node->left=leftpart[j];
node->right=rightpart[k];
res.push_back(node);
}
}
return res;
}
vector<TreeNode *> generateTrees(int n) {
return generateRes(1,n); }
};
Unique Binary Search Trees I&&II(II思路很棒)——动态规划(II没理解)的更多相关文章
- 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 I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II
1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...
- 96. Unique Binary Search Trees(I 和 II)
Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For example ...
- 【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) ...
- LeetCode: Unique Binary Search Trees II 解题报告
Unique Binary Search Trees II Given n, generate all 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 ...
随机推荐
- mybaties分页
首先引入jar包: <dependency> <groupId>com.github.pagehelper</groupId> <artifactId> ...
- jquery实现奇偶行赋值不同css值
<html> <head> <title>jquery奇偶行css效果</title> <script src="../../jquer ...
- 关于android中PendingIntent.getBroadcase的注册广播
使用语句 PendingIntent intent= PendingIntent.getBroadcast(Context context, int requestCode, Intent inten ...
- 2050年这些职业将逐渐被AI(人工智能)取代
耳熟能详的人工智能 深蓝Deep Blue是美国IBM公司生产的一台超级国际象棋电脑,重1270公斤,有32个大脑(微处理器),每秒钟可以计算2亿步."深蓝”输入了一百多年来优秀棋手的对 ...
- POJ 1014 / HDU 1059 Dividing 多重背包+二进制分解
Problem Description Marsha and Bill own a collection of marbles. They want to split the collection a ...
- 洛谷P3764 签到题 III
题目背景 pj组选手zzq近日学会了求最大公约数的辗转相除法. 题目描述 类比辗转相除法,zzq定义了一个奇怪的函数: typedef long long ll; ll f(ll a,ll b) { ...
- bzoj 2786 DP
我们可以将=左右的两个数看成一个块,块内无顺序要求,把<分隔的看成两个块,那么我们设w[i][j]代表将i个元素分成j个块的方案数,那么显然w[i][j]=w[i-1][j]*j+w[i-1][ ...
- .net WebAPI返回xml、json格式
WebAPI返回xml.json格式简单示例 using System.Net.Http.Formatting; public class TestController : ApiController ...
- linux下 vi中[noeol]以及出现 feff 的问题
"uptime.py" [noeol] 69L, 2311C"system/uptime.py" 69L, 2312C 'noeol' 就是 'no end-o ...
- 抓其根本(一)(hdu2710 Max Factor 素数 最大公约数 最小公倍数.....)
素数判断: 一.根据素数定义,该数除了1和它本身以外不再有其他的因数. 详见代码. int prime() { ; i*i<=n; i++) { ) //不是素数 ; //返回1 } ; //是 ...