题目:

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来分别接收左右子树。

代码:

  1. public List<TreeNode> generateTrees(int n){
  2. return generateTrees(1, n);
  3. }
  4.  
  5. public List<TreeNode> generateTrees(int begin, int end){
  6. List<TreeNode> arr = new ArrayList<TreeNode>();
  7. if(begin > end){
  8. return arr;
  9. }
  10. if(begin == end){
  11. TreeNode ptr = new TreeNode(begin);
  12. arr.add(ptr);
  13. return arr;
  14. }
  15. for(int i = begin; i <= end; i++){
  16. List<TreeNode> left = new ArrayList<TreeNode>();
  17. List<TreeNode> right = new ArrayList<TreeNode>();
  18. left = generateTrees(begin, i-1);
  19. right = generateTrees(i+1, end);
  20. //注意判断left和right是否为空
  21. //还有,要注意应该在最内层循环每次都新建根结点
  22. if(left.size() == 0){
  23. if(right.size() == 0){
  24. TreeNode root = new TreeNode(i);
  25. root.left = null;
  26. root.right = null;
  27. arr.add(root);
  28. }else{
  29. for(TreeNode r: right){
  30. TreeNode ptr = new TreeNode(i);
  31. ptr.left = null;
  32. ptr.right = r;
  33. arr.add(ptr);
  34. }
  35. }
  36. }else{
  37. if(right.size() == 0){
  38. for(TreeNode l: left){
  39. TreeNode ptr = new TreeNode(i);
  40. ptr.left = l;
  41. ptr.right = null;
  42. arr.add(ptr);
  43. }
  44. }else{
  45. for(TreeNode l: left){
  46. for(TreeNode r: right){
  47. TreeNode ptr = new TreeNode(i);
  48. ptr.left = l;
  49. ptr.right = r;
  50. arr.add(ptr);
  51. }
  52. }
  53. }
  54. }
  55. }
  56. return arr;
  57. }

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. egrep 查找IP

    1. egrep '([^0-9]|\<)(([0-1]?[0-9]{0,2}|([2]([0-4][0-9]|[5][0-5])))\.){3}([0-1]?[0-9]{0,2}|([2]([ ...

  2. Delphi 如何清除动态数组的内存?

    SetLength(glb_IndexConfig,); FreeAndNil(glb_IndexConfig);

  3. common.js

    //检测浏览器 function checkb(){ var Sys = {}; var ua = navigator.userAgent.toLowerCase(); var s; (s = ua. ...

  4. JS使用ActiveXObject读取数据库代码示例(只支持IE)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. yii2 rbac-plus的使用

    前言 1.本教程适合有RBAC基础,对RBAC有一定了解的同学. 2.本教程使用advanced模板 3.确保数据库中存在user表,没有的同学请查阅文档 运行 php yii migrate 来生成 ...

  6. iOS新建项目文件管理规范

    当我们进入到新的公司的第一天,看到以前老员工编写的代码,找个东西累死人咧,那个抓耳挠腮的啊,一般情况下都有想揍人的赶脚. 哈哈,不忙,先想一下自己的代码!想一下自己写的代码怎么才能新来的人一眼就能看懂 ...

  7. php+mysql实现事务回滚

    模拟条件:第一个表插入成功,但是第二个表插入失败,回滚.第一个表插入成功,第二个表插入成功,执行.第一个表插入失败,第二个表插入成功,回滚.第一个表插入失败,第二个表插入失败,回滚.以上情况都需要回滚 ...

  8. angularJS自定义属性作为条件中转

    <html> <head> <meta charset="utf-8"/> <title></title> </h ...

  9. [LeetCode] Combinations (bfs bad、dfs 递归 accept)

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  10. 实验 snort安装配置与规则编写

    1 实验目的 在linux或windows任意一个平台下完成snort的安装,使snort工作在NIDS模式下,并编写符合相关情景要求的snort规则. 2 实验环境 物理机:windows 8.1 ...