LeetCode: Unique Binary Search Trees II 解题报告
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
confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
Hide Tags Tree Dynamic Programming
SOLUTION 1:
使用递归来做。
1. 先定义递归的参数为左边界、右边界,即1到n.
2. 考虑从left, 到right 这n个数字中选取一个作为根,余下的使用递归来构造左右子树。
3. 当无解时,应该返回一个null树,这样构造树的时候,我们会比较方便,不会出现左边解为空,或是右边解为空的情况。
4. 如果说左子树有n种组合,右子树有m种组合,那最终的组合数就是n*m. 把这所有的组合组装起来即可
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; left = null; right = null; }
* }
*/
public class Solution {
public List<TreeNode> generateTrees(int n) {
// 0.07
return dfs(, n);
} public List<TreeNode> dfs(int left, int right) {
List<TreeNode> ret = new ArrayList<TreeNode>(); // The base case;
if (left > right) {
ret.add(null);
return ret;
} for (int i = left; i <= right; i++) {
List<TreeNode> lTree = dfs(left, i - );
List<TreeNode> rTree = dfs(i + , right);
for (TreeNode nodeL: lTree) {
for (TreeNode nodeR: rTree) {
TreeNode root = new TreeNode(i);
root.left = nodeL;
root.right = nodeR;
ret.add(root);
}
}
} return ret;
}
}
CODE:
LeetCode: 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] 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
题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...
- LeetCode——Unique Binary Search Trees II
Question Given an integer n, generate all structurally unique BST's (binary search trees) that store ...
- [Leetcode] 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 dfs 深度搜索
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- [leetcode]Unique Binary Search Trees II @ Python
原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees-ii/ 题意:接上一题,这题要求返回的是所有符合条件的二叉查找树,而上 ...
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many 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 ...
随机推荐
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- MariaDB5.5(mysql)的partitioning设置 for Zabbix3.0
用zabbix的同学都知道,一台服务器监视几百几千台服务器,一个服务器几十个item,长年下来数据量是很惊人的. 而zabbix自带的housekeeping功能,默认状态下的删除速度完全跟不上数据增 ...
- SQL Server 利用游标解决Tempdb究极竞争-DBA-程序员需知
SQL Server tempdb分配竞争算是DBA老生常谈的问题了,几乎现在所有的DBA都知道多建几个文件来解决/缓解问题.但是深层次的的竞争依旧不可避免.这里给大家剖析下游标在tempdb中的特点 ...
- 用c#开发微信 (17) 微活动 3 投票活动 (文本投票)
前面介绍了微活动<大转盘> 和 <刮刮卡>,这次介绍下微投票,微投票分二种,一种是文本投票, 一种是图片投票. 下面介绍文本投票的详细步骤: 1. 新建文本投票活动 ...
- C#设计模式(5)——建造者模式(Builder Pattern)
一.引言 在软件系统中,有时需要创建一个复杂对象,并且这个复杂对象由其各部分子对象通过一定的步骤组合而成.例如一个采购系统中,如果需要采购员去采购一批电脑时,在这个实际需求中,电脑就是一个复杂的对象, ...
- ajax异步请求Response.Redirect重定向
一个ajax异步请求报错->捕获异常->重定向错误提示页面. 一个简单的流程 结果一直搞不定.重定向无效.各种百度之. 后来突然想起 ajax的请求是不能在后台重定向的. 如果硬要重定向 ...
- KALI LINUX WEB 渗透测试视频教程—第16课 BEEF基本使用
Kali Linux Web 渗透测试视频教程—第16课 BeEF基本使用 文/玄魂 目录 Kali Linux Web 渗透测试视频教程—第16课 BeEF基本使用............... ...
- GUI 快捷键的实现思路
思路: 前提快捷键操作不可重复,即一个快捷键对应一个控件的动作 一个窗体保持一份快捷键的map映射 在相应的消息中获取快捷键列表如键盘消息 在控件类对象中定义一个默认的响应行为,比如Button按 ...
- Fragment之间的通信
在本节中,你会学到 1.定义接口 2.实现接口 3.将消息传递给fragment 为了重用Fragment UI 组件,在设计中你应该通过定义每一个fragemnt自己的layout和行为,让frag ...
- Windows 8.1 开发过程中遇到的小问题(2)
又是在Windows 8.1 的分享功能,再次出现错误: A COM call (IID: ***, method index: *) to an ASTA (thread *) was blocke ...