【leetcode】 Unique Binary Search Trees II (middle)☆
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
这次的题目要求是得到所有的树。
我的思路:
用f[n]存储1-n的所有方法的根节点
则 f[n+1] = 1作为根,f[0]做左子树,f[n]所有节点都加1做右子树 + 2作为根,f[1]做左子树,f[n - 1]所有节点都加2做右子树 +...
代码内存都没有释放,不过AC了。
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std; // 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) {
vector<vector<TreeNode *>> ans(n + , vector<TreeNode *>());
if(n == )
{
ans[].push_back(NULL);
return ans[];
} TreeNode * root = NULL;
ans[].push_back(root);
root = new TreeNode();
ans[].push_back(root);
for(int i = ; i <= n; i++) //总数字
{
for(int j = ; j < i; j++) //小于根节点的数字个数
{
for(int l = ; l < ans[j].size(); l++) //小于根节点的组成方法数
{
for(int r = ; r < ans[i - j - ].size(); r++) //大于根节点的组成方法数
{
TreeNode * root = new TreeNode(j + );
root->left = ans[j][l];
root->right = add(ans[i - j - ][r], j + ); //大于根节点的需要加上差值
ans[i].push_back(root);
}
}
}
} return ans[n];
} TreeNode * add(TreeNode * root, int Num)
{
if(root == NULL)
{
return root;
}
TreeNode * T = new TreeNode(root->val + Num);
T->left = add(root->left, Num);
T->right = add(root->right, Num); return T;
}
}; int main()
{
Solution s;
vector<TreeNode *> ans = s.generateTrees(); return ;
}
看别人的思路,把建立子树分为从数字start-end,直接递归求解,不需要像我的那样每次还要把右子树遍历增加值
/**
* 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*> generateTreesRec(int start, int end){
vector<TreeNode*> v;
if(start > end){
v.push_back(NULL);
return v;
}
for(int i = start; i <= end; ++i){
vector<TreeNode*> left = generateTreesRec(start, i - );
vector<TreeNode*> right = generateTreesRec(i + , end);
TreeNode *node;
for(int j = ; j < left.size(); ++j){
for(int k = ; k < right.size(); ++k){
node = new TreeNode(i);
node->left = left[j];
node->right = right[k];
v.push_back(node);
}
}
}
return v;
}
vector<TreeNode *> generateTrees(int n) {
return generateTreesRec(, n);
}
};
【leetcode】 Unique Binary Search Trees II (middle)☆的更多相关文章
- 【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 异构二叉查找树II
本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4048209.html 原题: Given n, generate all struc ...
- 【leetcode】Unique Binary Search Trees
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
- 【Leetcode】【Medium】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 (#96)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 【题解】【BST】【Leetcode】Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 【Leetcod】Unique Binary Search Trees II
给定结点数n,结点值为1,2,...,n,求由这些结点可以构成的所有二叉查找树. Given n, generate all structurally unique BST's (binary sea ...
- 【树】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 (middle)☆
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
随机推荐
- embed 层级太高
它怎么就好了,凭什么就好了.为什么就好了.我到底当时是哪里写错了.怎么个情况 兼容Firefox ,IE的flash透明和flash置底代码 <object classid="clsi ...
- HTTP状态301、404、200、304分别表示什么意思
301 (永久移动)请求的网页已永久移动到新位置.服务器返回此响应(对 GET 或 HEAD 请求的响应)时,会自动将请求者转到新位置.您应使用此代码告诉 Googlebot 某个网页或网站已永久移动 ...
- 必须知道的.net(性能条款)
以dispose的模式来代替finalize方式:非托管资源的清理主要有终止化操作和Dispose模式两种,其中Finalize方式存在执行时间不确定,运行顺序不确定,同时对垃圾回收的性能有极大的损伤 ...
- Spark之集群搭建
注意,这种安装方式是集群方式:然后有常用两种运行模式: standalone , on yarn 区别就是在编写 standalone 与 onyarn 的程序时的配置不一样,具体请参照spar2中的 ...
- linux 模块加载
错误: rmmod 时提示 rmmod: chdir(xxx): No such file or directory 解决方法: http://blog.csdn.net/luckywang1103/ ...
- javascript高级程序设计---Event对象三
进度事件 进度事件用来描述一个事件进展的过程,比如XMLHttpRequest对象发出的HTTP请求的过程.<img>.<audio>.<video>.<st ...
- CSS相邻兄弟选择器
相邻兄弟选择器定义:选择紧接在另一个元素后的元素,而且两者有着相同的父元素. 代码一:<body> <h1>This is a heading.</h1> < ...
- C++中的单例模式(转)
单例模式也称为单件模式.单子模式,可能是使用最广泛的设计模式.其意图是保证一个类仅有一个实例,并提供一个访问它的全局访问点,该实例被所有程序模块共享.有很多地方需要这样的功能模块,如系统的日志输出,G ...
- Codeforces Round #335 Sorting Railway Cars 动态规划
题目链接: http://www.codeforces.com/contest/606/problem/C 一道dp问题,我们可以考虑什么情况下移动,才能移动最少.很明显,除去需要移动的车,剩下的车, ...
- MongoDB的学习和使用(MongoDB GridFS)
MongoDB GridFS GridFS 用于存储和恢复那些超过16M(BSON文件限制)的文件(如:图片.音频.视频等). GridFS 也是文件存储的一种方式,但是它是存储在MonoDB的集合中 ...