leetcode95 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.

思路:
本题采取递归的思路。
传递的参数是开始数值(begin)和结束数值(end)。
当begin > end 时,返回空(注意不是null);
当begin == end 时, 返回含有 new TreeNode(begin)结点的ArrayList;
当begin < end时,建立两个ArrayList来分别接收左右子树。
代码:
public List<TreeNode> generateTrees(int n){
return generateTrees(1, n);
}
public List<TreeNode> generateTrees(int begin, int end){
List<TreeNode> arr = new ArrayList<TreeNode>();
if(begin > end){
return arr;
}
if(begin == end){
TreeNode ptr = new TreeNode(begin);
arr.add(ptr);
return arr;
}
for(int i = begin; i <= end; i++){
List<TreeNode> left = new ArrayList<TreeNode>();
List<TreeNode> right = new ArrayList<TreeNode>();
left = generateTrees(begin, i-1);
right = generateTrees(i+1, end);
//注意判断left和right是否为空
//还有,要注意应该在最内层循环每次都新建根结点
if(left.size() == 0){
if(right.size() == 0){
TreeNode root = new TreeNode(i);
root.left = null;
root.right = null;
arr.add(root);
}else{
for(TreeNode r: right){
TreeNode ptr = new TreeNode(i);
ptr.left = null;
ptr.right = r;
arr.add(ptr);
}
}
}else{
if(right.size() == 0){
for(TreeNode l: left){
TreeNode ptr = new TreeNode(i);
ptr.left = l;
ptr.right = null;
arr.add(ptr);
}
}else{
for(TreeNode l: left){
for(TreeNode r: right){
TreeNode ptr = new TreeNode(i);
ptr.left = l;
ptr.right = r;
arr.add(ptr);
}
}
}
}
}
return arr;
}
leetcode95 Unique Binary Search Trees II的更多相关文章
- LeetCode-95. Unique Binary Search Trees II
Description: Given n, generate all structurally unique BST's (binary search trees) that store values ...
- Leetcode95. Unique Binary Search Trees II不同的二叉搜索树2
给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树. 示例: 输入: 3 输出: [ [1,null,3,2], [3,2,null,1], [3,1,null,nul ...
- 【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) ...
- 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 解题报告
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- Unique Binary Search Trees,Unique Binary Search Trees II
Unique Binary Search Trees Total Accepted: 69271 Total Submissions: 191174 Difficulty: Medium Given ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- 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 ...
随机推荐
- 树莓派学Python博客收集
http://www.geekfan.net/8657/ 这个博客是控制LED,虽然不是原创地址不是这,但是我觉得排版比较好. http://my.oschina.net/RagingTyphoon ...
- 20145235 《Java程序设计》第6周学习总结
教材学习内容总结 10.1 InputStream与OutputStream 串流设计的概念 Java将输入/输出抽象化为串流,数据有来源及目的地,衔接两者的是串流对象. 从应用程序角度来看,如果要将 ...
- Bootstrap页面布局1 - 下载BS(bootstrap简称)
1.bootstrap 官方网站:http://wrongwaycn.github.io/bootstrap/docs/index.html 2.如图: 3.下载后得到如下目录结构 bootstrap ...
- P1017 进制转换
模拟水题,直接上代码 #include <bits/stdc++.h> using namespace std; const int maxn = 100000; int main() { ...
- (转)Linux下MatlabCompilerRuntime的安装和使用
1MCR简介 MCR之前是 Matlab Component Runtime的缩写,后更名为Matlab Compiler Runtime.MCR实际上是一组独立的共享库,也即是常说的动态连接库,所起 ...
- fatal error C1189: #error : WINDOWS.H already included. MFC apps must not #include <windows.h>
给对话框添加类, 报错 CalibrateMFCDlg.h(6) : error C2504: “CDialog”: 未定义基类 等多个错误 加上 #include "afxwin.h&qu ...
- UI---startup--jquery
http://www.w3school.com.cn 传统的基于表单提交, 整页刷新式的并不需要前端MVC. 当 然这种体验会很糟糕.试想一下, 用WebQQ时,每发一次消息页面就要泛白一次, 这是什 ...
- 常用ARM汇编指令
常用ARM汇编指令 [日期:2012-07-14] 来源:Linux社区 作者:xuyuanfan77 [字体:大 中 小] 在嵌入式开发中,汇编程序常常用于非常关键的地方,比如系统启动时初 ...
- Angular自动双向绑定值
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> ...
- mysql存储过程之游标遍历数据表
原文:mysql存储过程之游标遍历数据表 今天写一个mysql存储过程,根据自己的需求要遍历一个数据表,因为对存储过程用的不多,语法不甚熟悉,加之存储过程没有调试环境,花了不少时间才慢慢弄好,故留个痕 ...