leetcode先刷_Unique Binary Search Trees II
可能没想到,人的简单方法,关于质询的问题提出做。
如何把产生出来的所有的树木?所使用的方法当然是递归,但是有一个致命的问题,假设根节点,然后做一个递归,所以这是非常多的公共树木的根,结果肯定是一团糟。
怎么办?事实上,在思想上先实践的数量目前正在寻求高度统一,先把全部的左右子树都求出来。然后把它们之间的全部组合都连接到一个新建立出来的根节点,既然是分开左右子树。非常easy想到类似二分的思想。每次指定的不是一个位置。而是一个范围。我一開始还想着先把n个数放到数组里面再递归,脱了裤子放屁啊。
通过对算法思想的描写叙述,“先确定左右子树,再加入根节点”。非常easy想到使用类似后序遍历的编码格式。
class Solution {
public:
vector<TreeNode *> buildTrees(int beg, int end){
vector<TreeNode *> res, left, right;
if(beg>end){
res.push_back(NULL);
return res;
}
for(int i=beg;i<=end;i++){
left = buildTrees(beg, i-1);
right = buildTrees(i+1, end);
for(int j=0;j<left.size();j++){
for(int k=0;k<right.size();k++){
TreeNode *root = new TreeNode(i+1);
root->left = left[j];
root->right = right[k];
res.push_back(root);
}
}
}
return res;
}
vector<TreeNode *> generateTrees(int n) {
return buildTrees(0, n-1);
}
};
版权声明:本文博客原创文章,博客,未经同意,不得转载。
leetcode先刷_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】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] 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] 95. Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- 【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 ...
- leetcode 95 Unique Binary Search Trees II ----- java
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- 【LeetCode】Unique Binary Search Trees II 异构二叉查找树II
本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4048209.html 原题: Given n, generate all struc ...
随机推荐
- ignore,neglect,omit,overlook
一:简介——ignore :通常指有意不顾,或不理显而易见的事物.neglect :侧重指有意的忽略或忽视,也可指粗心与疏忽.omit :指有意或无意地忘记做某事,也指删去被视作不重要.不合意的东西. ...
- java.lang.IllegalStateException: ActionBarImpl can only be used with a compatible window decor layou
于Activity调用它们的定义dialog事件ActionBarImpl can only be used with a compatible window decor layout异常, 解决方法 ...
- HihoCoder——Trie树
本文出自:http://blog.csdn.net/svitter 原题:http://hihocoder.com/contest/hiho2/problem/1 题解:使用Trie树..基础题目.一 ...
- ACM-DP最大连续子——hdu1231
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- Vijos.1096 津津储蓄计划
见问题: https://vijos.org/p/1096 主题概述 津津的零花钱一直都是自己的管理.每月初的母亲津津300美元,津津将于本月支出预算.而且总是做同样的实际支出与预算. 为了让津津学 ...
- hadoop工作平台梳理
文章 http://blog.csdn.net/lili72/article/details/41130743 lili72 数据平台: 一. hadoop平台:Hbase.hive,storm,s ...
- unity3D实际的原始视频游戏开发系列讲座10它《战斗:外来入侵》在第一季度游戏开发
解说文件夹 <保卫战:异形入侵>游戏开发 第一讲 游戏演示和资源的介绍 第二讲 "异形"怪物的实现 第三讲 "异形"怪物生命值的体现 第四讲 ...
- hibernate学习笔记(1)hibernate基本步骤
hibernate基本步骤 1.创hibernate置对象 Configuration config = newConfiguration(); config.configure("hibe ...
- 网络编程I/O功能介绍
read和write #include <unistd.h> ssize_t read(int fd, void *buf, size_t count); ssize_t write(in ...
- 网络资源(3) - iBatis视频
2018_08_24 http://v.youku.com/v_show/id_XMjk2ODY2OTE2.html iBatis视频教程01