【Leetcode】【Medium】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
解题:
按照动态规划的思路,求1~n的全部排列:
遍历1~n中的每一个数k,作为根结点,所有比k小的数放在左子树,比k大的数放在右子树;
则以k为根节点的1~n全排列 等于 k的左子树全排列 与 k的右子树全排列的组合;
由此使用递归的方法,得到所有结果。
注意,递归只需考虑三个情况:
1、确定好递归函数返回值的意义,只关注当前层的逻辑,思维不要一层一层跟着递归深入;
2、设置好门限条件,也就是递归最底层的基础返回条件;
3、防止出现无限递归,无法跳出递归层;
代码:
/**
* 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) {
return __generateTrees(, n);
} vector<TreeNode *> __generateTrees (int begin, int end) {
vector<TreeNode *> cur_nodes_mathods;
if (begin > end) {
cur_nodes_mathods.push_back(NULL);
return cur_nodes_mathods;
} for (int i = begin; i <= end; ++i) {
vector<TreeNode *> lefts = __generateTrees(begin, i - );
vector<TreeNode *> rights = __generateTrees(i + , end); for (int j = ; j < lefts.size(); ++j) {
for (int k = ; k < rights.size(); ++k) {
TreeNode *cur_root = new TreeNode(i);
cur_root->left = lefts[j];
cur_root->right = rights[k];
cur_nodes_mathods.push_back(cur_root);
//delete cur_root;
}
}
} return cur_nodes_mathods;
}
};
【Leetcode】【Medium】Unique Binary Search Trees II的更多相关文章
- 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)
[LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
- 【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] 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解题报告—— 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 ...
- leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses
96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...
- Unique Binary Search Trees,Unique Binary Search Trees II
Unique Binary Search Trees Total Accepted: 69271 Total Submissions: 191174 Difficulty: Medium Given ...
- 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 II 异构二叉查找树II
本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4048209.html 原题: Given n, generate all struc ...
随机推荐
- 使用vmware虚拟机安装linux
- Centos 7网卡设置
#yum install net-tools.x86_64 #cd /etc/sysconfig/network-scripts/ #mv ifcfg-eno16780032 ifcfg-eth0 手 ...
- tar压缩命令
01-.tar格式解包:[*******]$ tar xvf FileName.tar打包:[*******]$ tar cvf FileName.tar DirName(注:tar是打包,不是压缩! ...
- ant如何编译项目
Ant的概念 可能有些读者并不理解什么是Ant以及如何使用它,但只要使用通过Linux系统的读者,应该知道make这个命令.当编译Linux内核及一些软件的源程序时,经常要用这个命令.Make命令其实 ...
- unity优化
1. 更新不透明贴图的压缩格式为ETC 4bit,因为android市场的手机中的GPU有多种,每家的GPU支持不同的压缩格式,但他们都兼容ETC格式, 2. 对于透明贴图,我们只能选择RGBA 16 ...
- SQL Cookbook—查询、排序
涉及到的问题1.在select语句中使用条件逻辑2.限制返回的行数3.从表中随机返回n条记录4.将空值转换为实际值5.对字母和数字混合的数据排序6.处理排序空值7.根据数据项的键排序–8.从一个表中查 ...
- JQuery JSON Servlet
<script type="text/javascript" src="js/jquery-1.10.2.js"></script> & ...
- Mybatis缓存(一)
1.什么是缓存 Mybatis提供缓存,用于减轻数据压力,提高数据库性能. 2.Mybatis缓存分类 Mybatis的缓存分为一级缓存和二级缓存. Mybatis的一级缓存 1.一级缓存的范围 1 ...
- .NET环境下的DPAPI加密编程
Windows的本地加密保护机制提供了简单的调用接口,密钥的生成.保护等事项一概由系统来处理,其编程接口称为DPAPI.这一加密保护机制的边界是用户登录帐户或者本地计算机系统,使用操作系统设定的加密处 ...
- hihocoder #1529 : 不上升序列
Description 给定一个长度为 n 的非负整数序列 a[1..n]. 你每次可以花费 1 的代价给某个 a[i] 加1或者减1. 求最少需要多少代价能将这个序列变成一个不上升序列. Solut ...