题目:

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. MVC4发布到IIS7报404错误

    在MVC根目录的web.config中添加 <system.webServer> <modules runAllManagedModulesForAllRequests=" ...

  2. 【IOS笔记】View Controller Basics

    View Controller Basics   视图控制器基础 Apps running on iOS–based devices have a limited amount of screen s ...

  3. GUN485项目的总结

    1.DMA中配置要放在串口的配置后面. 2.DMA有3种中断方式:传输完成.传输一半.传输错误 3.如果要用DMA容易造成串口数据还没发完就把485的控制脚拉低导致数据没发完.解决办法是DMA发送完成 ...

  4. wordpress为dashboard添加样式

    function my_admin_theme_style() { wp_enqueue_style('my-admin-style', get_template_directory_uri() . ...

  5. Delphi中如何控制其他程序窗体上的窗口控件

    回调函数一般是按照调用者的要求定义好参数和返回值的类型,你向调用者提供你的回调函数的入口地址,然后调用者有什么事件发生的时候就可以随时按照你提供的地址调用这个函数通知你,并按照预先规定好的形式传递参数 ...

  6. android 定位的四种方式

    [原文]  开发中对于地图及地理位置的定位是我们经常要用地,地图功能的使用使得我们应用功能更加完善,下面总结了一下网络中现有对于介绍android定位的4种方式,希望对大家有帮助: android 定 ...

  7. 【转】Laravel+Angularjs+D3打造可视化数据,RESTful+Ajax

    http://897371388.iteye.com/blog/1975351 大致思路也就是下面,由于最近在学Laravel也在学Angularjs,加上之前做的项目用到了d3. 原来的方案如下: ...

  8. Js(javaScript)的闭包原理

    问题?什么是js(javaScript)的闭包原理,有什么作用? 一.定义 官方解释:闭包是一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分.  小编 ...

  9. 弹窗文件js+css

    // 每个弹窗的标识 var x =0; var idzt = new Array(); var Window = function(config){ //ID不重复 idzt[x] = " ...

  10. NSURL基本操作示例说明

    http://blog.csdn.net/zhibudefeng/article/details/7920686