题目:

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的更多相关文章

  1. LeetCode-95. Unique Binary Search Trees II

    Description: Given n, generate all structurally unique BST's (binary search trees) that store values ...

  2. Leetcode95. Unique Binary Search Trees II不同的二叉搜索树2

    给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树. 示例: 输入: 3 输出: [   [1,null,3,2],   [3,2,null,1],   [3,1,null,nul ...

  3. 【LeetCode】95. Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  4. 【leetcode】Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

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

  6. LeetCode: Unique Binary Search Trees II 解题报告

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  7. Unique Binary Search Trees,Unique Binary Search Trees II

    Unique Binary Search Trees Total Accepted: 69271 Total Submissions: 191174 Difficulty: Medium Given  ...

  8. [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆

    Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...

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

随机推荐

  1. Oracle 语句常见错误

    Merge into的注意点之ORA-30926:无法在源表中获得一组稳定的行? merge into 的内部处理是将table_source 的每一条记录和table_target的每一条记录对比匹 ...

  2. 制作支持UEFI PC的Server2008 R2系统安装U盘

    下载R2系统 ISO ed2k://|file|cn_windows_server_2008_r2_standard_enterprise_datacenter_and_web_with_sp1_x6 ...

  3. php 文件和表单内容一起上传

    <?php $filename = $_POST['filename']; $explain = $_POST['explain']; $upfile = $_FILES['upfile']; ...

  4. Programs vs. processes

    Computer Systems A Programmer's Perspective Second Edition This is a good place to pause and make su ...

  5. improve performance whilemaintaining the functionality of a simpler and more abstract model design of processor hardware

    Computer Systems A Programmer's Perspective Second Edition In this chapter, we take a brief look at ...

  6. day08

      软件系统体系结构   常见软件系统体系结构B/S.C/S 1.1 C/S C/S结构即客户端/服务器(Client/Server),例如QQ: 需要编写服务器端程序,以及客户端程序,例如我们安装的 ...

  7. PHP数据库基础

    PHP数据库基础: 1.varchar:字符串,用于姓名班级,地址等,地址一般长50,姓名长20 2.int:整数,用于成绩,序号等 3.float:小数 4.bit:布尔型,用于性别等 5.时间也用 ...

  8. C# Unicode编码与解码方法

    public static class ExtentMethod { public static string ToUnicodeString(this string str) { StringBui ...

  9. 002_base64的编码实现

    一.先贴一张ASCII码的图 二.再贴一张base64转换规则的图 二.python代码实现.

  10. CDH(Cloudera)与hadoop(apache)对比

    本文出自:CDH(Cloudera)与hadoop(apache)对比http://www.aboutyun.com/thread-9225-1-1.html(出处: about云开发)   问题导读 ...