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 ...
随机推荐
- How to do logging in C# with log4net
If you are writing server code in C# or to a lesser extent desktop/client then it's a good idea to i ...
- 上线踩坑引发的处理方式---lsof,strace
两个跟踪进程的linux命令 lsof -p pidstrace -p pid快速跟踪进程出现问题的地方.由于进程本身运行良好,但是内部一直等待第三方哪个应答,导致进程假死.引用自:http://li ...
- nginx+php 安装手册
http://www.cnblogs.com/hxl2009/archive/2013/06/11/3131627.html [mysql安装] php 安装 1: wget http://ftp. ...
- 在CS代码页获取input输入框内肉----.net学习点滴
想在后台cs页面得到前台页面aspx中html控件input输入的值.通过访问input输入框的name属性值获取. 解决方法如下: 1.用Request["user"].toSt ...
- C#设计模式(9)——装饰者模式(Decorator Pattern)
一.引言 在软件开发中,我们经常想要对一类对象添加不同的功能,例如要给手机添加贴膜,手机挂件,手机外壳等,如果此时利用继承来实现的话,就需要定义无数的类,如StickerPhone(贴膜是手机类).A ...
- [WinAPI] API 5 [遍历驱动器并获取驱动器属性]
(1) GetLogicalDrives.获取主机中所有的逻辑驱动器,以BitMap的形式返回.◇返回值GetLogicalDrive函数返回一个DWORD类型的值,第一位表示所对应的驱动器是否存在. ...
- 一道印象深刻的面试题:String参数传递问题
今天小菜去北京某知名公司面试,做了公司的面试题,然后就是轻松的面试. 面试过程中,面试官让我讲讲其中一个题是怎么选的答案,代码大致内容如下: public class StringTest{ publ ...
- java5 Lock用法
锁是控制多个线程对共享资源进行访问的工具.通常,锁提供了对共享资源的独占访问.一次只能有一个线程获得锁,对共享资源的所有访问都需要首先获得锁.不过,某些锁可能允许对共享资源并发访问,如 ReadWri ...
- 老生常谈: Javascript 面向对象编程初探(一)--- 封装
Javascript是一种基于对象(object-based)的语言,你遇到的所有东西几乎都是对象.但是,它又不是一种真正的面向对象编程(OOP)语言,因为它的语法中没有class(类). 那么,如果 ...
- python数据采集与多线程效率分析
以前一直使用PHP写爬虫,用Snoopy配合simple_html_dom用起来也挺好的,至少能够解决问题. PHP一直没有一个好用的多线程机制,虽然可以使用一些trick的手段来实现并行的效果(例如 ...