LeetCode OJ——Unique Binary Search Trees II
http://oj.leetcode.com/problems/unique-binary-search-trees-ii/
一题要求得出所有树的种类数,二题要求得出所有树。
在一题的基础上修改代码,还是要提前想清楚再写。
#include <iostream>
#include <vector>
using namespace std; struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}; class Solution {
public:
vector<TreeNode *> fun(int start,int end )
{
vector<TreeNode *> ansTemp;
vector<TreeNode *> ansTemp2;
vector<TreeNode *> ansTemp3; ansTemp.clear();
if(start>end)
{
ansTemp.push_back(NULL);
return ansTemp;
}
if(start == end)
{
TreeNode *mid = new TreeNode(start);
ansTemp.push_back(mid);
return ansTemp;
} for(int i=start;i<=end;i++)//枚举的中间位置
{
ansTemp2 = fun(start,i-);
ansTemp3 = fun(i+,end); if(ansTemp2.size()!=&&ansTemp3.size()!=)
for(int j = ;j<ansTemp2.size();j++)
{
for(int k = ;k<ansTemp3.size();k++)
{
TreeNode * root = new TreeNode();
root->val = i;
root->left = ansTemp2[j];
root->right = ansTemp3[k];
ansTemp.push_back(root);
}
}
else if(ansTemp2.size()!=&&ansTemp3.size()==)
for(int j = ;j<ansTemp2.size();j++)
{
TreeNode * root = new TreeNode();
root->val = i;
root->left = ansTemp2[j];
root->right = NULL;
ansTemp.push_back(root);
}
else if(ansTemp2.size()==&&ansTemp3.size()!=)
for(int k = ;k<ansTemp3.size();k++)
{
TreeNode * root = new TreeNode();
root->val = i;
root->left = NULL;
root->right = ansTemp3[k];
ansTemp.push_back(root);
}
else
{
TreeNode * root = new TreeNode(i);
ansTemp.push_back(root);
} } return ansTemp;
} 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<TreeNode *> ans;
ans.clear();
ans = fun(,n);
return ans;
}
}; int main()
{
Solution my;
my.generateTrees();
return ;
}
亲爱的,加油。
LeetCode OJ——Unique Binary Search Trees II的更多相关文章
- [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 唯一二叉搜索树 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]95. Unique Binary Search Trees II给定节点形成不同BST的集合
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- Java for LeetCode 095 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 异构二叉查找树II
本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4048209.html 原题: Given n, generate all struc ...
- [LeetCode] 95. Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- leetCode 95.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 (middle)☆
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
随机推荐
- Java--equals和 == 的比较和equals()、HashCode()的重写
一. equals和 == 的比较 1.== 运算符 ① == 如果比较的是基本数据类型,则比较的是值. ② == 如果比较的是引用数据类型,则比较的是地址值. 2.equals ①它属于java.l ...
- vue组件:canvas实现图片涂鸦功能
方案背景 需求 需要对图片进行标注,导出图片. 需要标注N多图片最后同时保存. 需要根据多边形区域数据(区域.颜色.名称)标注. 对应方案 用canvas实现涂鸦.圆形.矩形的绘制,最终生成图片bas ...
- angular5自适应窗口大小
import {AfterViewInit, Directive, ElementRef, HostBinding, HostListener, Inject, Input, Renderer2} f ...
- python解析库之 XPath
1. XPath (XML Path Language) XML路径语言 2. XPath 常用规则: nodename 选取此节点的所有子节点 / 从当前 ...
- 数字pid笔记(1)
针对stm32中可以如下实现: p->IncrementVal = (p->Kp * (p->err - p->err_next)) + (p->Ki * p->e ...
- 逃离迷宫 HDU - 1728(bfs)
逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- Android兼容性测试CTS --环境搭建、测试执行、结果分析
为了确保Android应用能够在所有兼容Android的设备上正确运行,并且保持相似的用户体验,在每个版本发布之时,Android提供了一套兼容性测试用例集合(Compatibility Test S ...
- Ubuntu首次登入与在线求助man page总结
1.为了避免瞬间断电造成的Linux系统的危害,建议做为服务器的Linux主机应该加上不断电系统来持续提供稳定的电力 2.默认的图形模式登入中,可以选择语系以及作业阶段.作业阶段为多种窗口管理员软件所 ...
- PHP全栈开发
DAY01_PHP基础第一天 01.了解php 00:09:26 ★ 02.php的开发环境准备 00:13:47 ★ 03.人人都会编程 00:10:26 ...
- git status 下中文显示乱码问题解决
$ git status -s ?? "\350\257\264\346\230\216.txt\n $ printf & ...