【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 使用缓存加快运算
from functools import lru_cache import time from functools import wraps def clock(func): @wraps(func ...
- 性能测试工具Jmeter12-Jmeter连接配置带跳板机(SSH)的mysql服务器
准备工作: 1.下载mysql-connector-java-5.1.13-bin.jar包,然后放在jmeter安装目录lib下 2.重新打开jmeter即可 要完成以上两步才能进行后续操作 要点: ...
- jQuery练习 | 提交表单验证
执行函数时,raturn false可阻止标签(例如超链接)的事件发生,从而达到提交表单的效果 <!DOCTYPE html> <html lang="en"&g ...
- 案例20-页面使用redis缓存显示类别菜单
1 准备工作 1 需要导入所需要的jar包. 2 启动windows版本的redis服务端 3 准备JedisUtils工具类的配置文件redis.properties redis.maxIdle= ...
- Android控件之ListView的使用
ListView是Android当中一个非常常用的数据显示控件. 第一种可以使用List<HashMap<String , Object>>,作为适配器的数据源来显示要显示的数 ...
- Jedis连接redis
今天与大家分享下,Jedis连接池使用.先看一段JAVA 代码: JedisPoolConfig config = new JedisPoolConfig(); con ...
- dozer 简单用法
maven添加必要的库: <!-- https://mvnrepository.com/artifact/net.sf.dozer/dozer --> <dependency> ...
- 跨平台 GUI可视化 网络调试工具
mNetAssisthttp://blog.chinaunix.net/uid-21977056-id-4310527.htmlhttps://github.com/busyluo/mNetAssis ...
- Kettle系列文章二(安装配置Kettle+SqlServer+简单的输入输出作业)
一.下载 Kettle下载地址:https://community.hitachivantara.com/docs/DOC-1009855 下拉到DownLoad,点击红框中的链接进行下载.. 二.解 ...
- 关于responseHeader的一些基础设置
1.关于响应头的一些基础设置 //设置相应头 response.addHeader("name","zhangsan"); response.addIntHea ...