【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 ...
随机推荐
- python 流式游标读取mysql大型数据库
import asyncio import aiomysql async def dbdaochu(loop): sqlstr='sql' conn = await aiomysql.connect( ...
- word-wrap:表示是否允许流浪器断句,word-break:表示怎样断句
word-wrap: break-word的话,流浪器可以断句,但是是按单词形式断句. 而加上 word-break: break-all的话,单词内部也断句. "whiteSpace&qu ...
- springboot整合jsp踩坑
springboot以其高效的开发效率越来越多的用在中小项目的开发,并且在分布式开发中的使用也很广泛,springboot官方推荐的前端框架却是thymeleaf,并且默认不支持jsp,而大部分jav ...
- 高效的JS数组操作
1.向数组的末尾添加元素 var arr=[1,2,3]; arr[arr.length]=1; 2.向数组的头部添加元素 var arr=[1,2,3]; [0].concat(arr); 3.向数 ...
- sql根据表中数量字段自动复制记录行
客户需要将表中统计好的数据还原成统计前的原始记录 例如: ID Name QTYCount100 Name1 1101 Name2 2102 Name3 3103 Name4 4104 Name5 5 ...
- android 仿网易新闻首页框架
实现思路很简单左侧栏目是一个一个的 Fragment 的,点击时动态替换各个 Fragment 到当前 Activity 中. 关键代码: public void loadFragment(Ma ...
- HttpSessionListener
1 知识点
- 如何使标签a处于不可用状态
今天做项目的时候突然发现a标签下用disabled无法使它的点击事件失效(貌似ie下可以,没有测试过), 首先说一下项目要求,点击a标签(点击之后以防多次快速点击,这里需要点击后使标签a实现),触发a ...
- 2018年javaee学习目标
我打算在本学期的学习中把java ee掌握并能熟练应用,如果可以的话还打算编写一个后台服务程序,增加自己的实战经验.
- nginx基本配置说明
nginx基本配置与参数说明 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2 ...