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

题解:递归的枚举1~n的每个节点为根节点,然后递归的利用它左边的节点构造左子树,放在一个list里面;再利用它右边的节点构造右子树,也放在一个list里面;最终枚举两个list里面的左子树和右子树,构建一棵树。

代码如下:

 /**
* 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 {
private ArrayList<TreeNode> generate(int start,int end){
ArrayList<TreeNode> answer = new ArrayList<TreeNode>();
if(start > end){
answer.add(null);
return answer;
} //for every node from start to right,make it as tree root and recursively build its left and right child tree
for(int i = start; i <= end;i++){
ArrayList<TreeNode> left = generate(start, i-1);
ArrayList<TreeNode> right = generate(i+1, end);
for(TreeNode l:left){
for(TreeNode r:right){
TreeNode root = new TreeNode(i);
root.left = l;
root.right = r;
answer.add(root);
}
} } return answer; }
public List<TreeNode> generateTrees(int n) {
return generate(1,n);
}
}

【leetcode刷题笔记】Unique Binary Search Trees II的更多相关文章

  1. LeetCode(95) Unique Binary Search Trees II

    题目 Given n, generate all structurally unique BST's (binary search trees) that store values 1-n. For ...

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

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

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

  4. leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses

    96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...

  5. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

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

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

  7. 【leetcode】Unique Binary Search Trees II

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

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

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

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

  10. Unique Binary Search Trees,Unique Binary Search Trees II

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

随机推荐

  1. mongoDB: cursor not found on server

    查询mongoDB集合数据更新,数据有400w多.我一次用cursor(游标)取1w,处理更新.程序在某段时间运行中遍历游标时发生异常! DBCursor cursor = tabColl.find( ...

  2. 让UITableView进入编辑模式

    1.UITableView对象有一个editing属性,设为YES时,该对象会进入编辑模式(editing mode).表格视图进入编辑模式后,用户可以管理表格中得行,如改变行的排列顺序.增加行或删除 ...

  3. 走进windows编程的世界-----消息处理函数(3)

    二 定时器消息 1 定时器消息 WM_TIMER   依照定时器设置时间段,自己主动向窗体发送一个定时器消息WM_TIMER. 优先级比較低.   定时器精度比較低,毫秒级别.消息产生时间也精度比較低 ...

  4. Eclipse个最实用的快捷键

    一个Eclipse骨灰级开发人员总结了他觉得最实用但又不太为人所知的快捷键组合.通过这些组合能够更加easy的浏览源码,使得总体的开发效率和质量得到提升.     1. ctrl+shift+r:打开 ...

  5. brew Error: Formulae found in multiple taps

    Mac PHP brew install php56-apcu Error: Formulae found in multiple taps: * homebrew/php/php56-apcu * ...

  6. 企业级Nginx服务基础到架构优化详解

    1.隐藏nginx header版本号 2.更改源码隐藏软件名称 3.更改nginx默认用户及用户组 4.配置nginx worker进程个数 5.根据CPU核数进行nginx进程优化 6.nginx ...

  7. 怎样新建Quartusproject—FPGA新手教程

    这一章我们来实现第一个FPGAproject-LED流水灯.我们将通过流水灯例程向大家介绍一次完整的FPGA开发流程,从新建project,代码设计,综合实现.管脚约束,下载FPGA程序. 掌握本章内 ...

  8. python unittest不执行"if __name__ == '__main__' "问题(Pycharm)

    问题: 1.selenium导入unittest框架和HtmlReport框架后,HtmlReport不被执行. 2.IDE为Pycharm 假设代码为: from selenium import w ...

  9. 安装wamp后 异常Exception Exception in module wampmanager.exe at 000F15A0

    系统环境:Windows 2008 R2 64bit 安装环境:wampserver2.4-x64 按照正常windows安装程序,完成WAMP Server程序安装,安装完成启动WAMP Serve ...

  10. PropertyUtils复制BigDecimal异常

    PropertyUtils复制BigDecimal会引发异常,要注意