LeetCode:Unique Binary Search Trees I II
LeetCode: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
分析:依次把每个节点作为根节点,左边节点作为左子树,右边节点作为右子树,那么总的数目等于左子树数目*右子树数目,实际只要求出前半部分节点作为根节点的树的数目,然后乘以2(奇数个节点还要加上中间节点作为根的二叉树数目)
递归代码:为了避免重复计算子问题,用数组保存已经计算好的结果
class Solution {
public:
int numTrees(int n) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int nums[n+]; //nums[i]表示i个节点的二叉查找树的数目
memset(nums, , sizeof(nums));
return numTreesRecur(n, nums);
}
int numTreesRecur(int n, int nums[])
{
if(nums[n] != )return nums[n];
if(n == ){nums[] = ; return ;}
int tmp = (n>>);
for(int i = ; i <= tmp; i++)
{
int left,right;
if(nums[i-])left = nums[i-];
else left = numTreesRecur(i-, nums);
if(nums[n-i])right = nums[n-i];
else right = numTreesRecur(n-i, nums);
nums[n] += left*right;
}
nums[n] <<= ;
if(n % != )
{
int val;
if(nums[tmp])val = nums[tmp];
else val = numTreesRecur(tmp, nums);
nums[n] += val*val;
}
return nums[n];
}
};
非递归代码:从0个节点的二叉查找树数目开始自底向上计算,dp方程为nums[i] = sum(nums[k-1]*nums[i-k]) (k = 1,2,3...i)
class Solution {
public:
int numTrees(int n) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int nums[n+]; //num[i]表示i个节点的二叉查找树数目
memset(nums, , sizeof(nums));
nums[] = ;
for(int i = ; i <= n; i++)
{
int tmp = (i>>);
for(int j = ; j <= tmp; j++)
nums[i] += nums[j-]*nums[i-j];
nums[i] <<= ;
if(i % != )
nums[i] += nums[tmp]*nums[tmp];
}
return nums[n];
}
};
LeetCode: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
按照上一题的思路,我们不仅仅要保存i个节点对应的BST树的数目,还要保存所有的BST树,而且1、2、3和4、5、6虽然对应的BST数目和结构一样,但是BST树是不一样的,因为节点值不同。
我们用数组btrees[i][j][]保存节点i, i+1,...j-1,j构成的所有二叉树,从节点数目为1的的二叉树开始自底向上最后求得节点数目为n的所有二叉树 本文地址
/**
* 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) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
vector<vector<vector<TreeNode*> > > btrees(n+, vector<vector<TreeNode*> >(n+, vector<TreeNode*>()));
for(int i = ; i <= n+; i++)
btrees[i][i-].push_back(NULL); //为了下面处理btrees[i][j]时 i > j的边界情况
for(int k = ; k <= n; k++)//k表示节点数目
for(int i = ; i <= n-k+; i++)//i表示起始节点
{
for(int rootval = i; rootval <= k+i-; rootval++)
{//求[i,i+1,...i+k-1]序列对应的所有BST树
for(int m = ; m < btrees[i][rootval-].size(); m++)//左子树
for(int n = ; n < btrees[rootval+][k+i-].size(); n++)//右子树
{
TreeNode *root = new TreeNode(rootval);
root->left = btrees[i][rootval-][m];
root->right = btrees[rootval+][k+i-][n];
btrees[i][k+i-].push_back(root);
}
}
}
return btrees[][n];
}
};
【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3448569.html
LeetCode:Unique Binary Search Trees I II的更多相关文章
- [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- LeetCode: Unique Binary Search Trees II 解题报告
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 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 ...
- LeetCode - Unique Binary Search Trees II
题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...
- 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 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- Unique Binary Search Trees I & II
Given n, how many structurally unique BSTs (binary search trees) that store values 1...n? Example Gi ...
- LeetCode——Unique Binary Search Trees II
Question Given an integer n, generate all structurally unique BST's (binary search trees) that store ...
- [Leetcode] Unique binary search trees ii 唯一二叉搜索树
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
随机推荐
- python启动服务器
3.* python -m http.server [port] & 2.* python -m SimpleHTTPServer [port] ...
- python代码学习day03-序列化学习pickle及json
#!/usr/bin/env python #coding:utf8 import pickle,json import datetime dic1 = {'name':'alex', 'age':4 ...
- ELK 信息统计分析-1
Aggregations 格式如下: "aggregations"{ //可以简写为aggs "<aggregation_name>":{ //名称 ...
- JavaScript 题目破解过程与解析
题目来源 https://www.hackthissite.org/missions/javascript/ HackThisSite JavaScript mission 1-7 1 我先尝试输入 ...
- django模型
用django时,只要用到数据库就得用到模型. 一.数据库的MTV开发模式 从MVC到MTV 所谓软件架构的MVC模式将数据的存取逻辑(Module),表现逻辑(View)和业务逻辑(Controll ...
- [转]GridView中直接新增行、编辑和删除
本文转自:http://www.cnblogs.com/gdjlc/archive/2009/11/10/2086951.html .aspx <div><asp:Button ru ...
- openfire+asmack搭建的安卓即时通讯(一) 15.4.7
最进开始做一些android的项目,除了一个新闻客户端的搭建,还需要一个实现一个即时通讯的功能,参考了很多大神成型的实例,了解到operfire+asmack是搭建简易即时通讯比较方便,所以就写了这篇 ...
- On Perseverance
Brothers,I dont consider that I have made it my own.But one thing I do:forgetting what lies behind a ...
- uva 1572 self-assembly ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAxQAAANxCAYAAAB9uv94AAAgAElEQVR4nOxdPW7tOpLWFrQGJb72vI ...
- android studio没有org.apache.http.client.HttpClient;等包问题 解决方案
以前用Eclipse做Android开发工具一直使用apache的http做网络请求,最近换用了Android studio发现没有办法引用apache的包,下面是我引用的步骤