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. php url路由伪静态

    RewriteEngine on RewriteBase /RewriteRule ^([a-zA-Z]{1,})/([a-zA-Z]{1,})$ webim2/operator/users.php? ...

  2. devpress grid表格自适应列宽的问题

    /// <summary> /// 自适应列宽,显示横向滚轴,只有当所有列都已经在界面上加载完成之后才能生效 /// </summary> public void setAut ...

  3. MVC| Razor 布局-模板页 | ViewStart.cshtml

    来自:http://blog.csdn.net/fanbin168/article/details/49725175 这个图就看明白了 _ViewStart.cshtml 视图文件的作用  _View ...

  4. .NET CORE 2.0小白笔记(二):VS插件 ReSharper Ultimate 2017 安装破解

    采用Use License Server方式激活,如果有多台要激活的可以用一个当服务器 下载网盘:链接: https://pan.baidu.com/s/1nvacvKP 密码: ipxc 版本:Je ...

  5. Godaddy域名 绑定ip 服务器

    比如我的域名是wmxl.info 第一个红框代表wmxl.info 绑定的 211.83.110.216 第一个代表www.wmxl.info 绑定的 211.83.110.216, 你也可以换一个服 ...

  6. java之JDK动态代理

    © 版权声明:本文为博主原创文章,转载请注明出处 JDK动态代理: JDK动态代理就是在程序运行期间,根据java的反射机制自动的帮我们生成相应的代理类 优势: - 1. 业务类只需要关注业务逻辑本身 ...

  7. Android SDK环境搭建

    方法有二 方法一: Android SDK开发包国内下载地址 http://www.cnblogs.com/bjzhanghao/archive/2012/11/14/android-platform ...

  8. 解决myeclipse10.x的Servers产生的at com.genuitec.eclipse.ast.deploy.core.Deployment.<init>(Unknown Source)错

    错误: java.lang.NullPointerException at com.genuitec.eclipse.ast.deploy.core.Deployment.<init>(U ...

  9. 【Mac + Python】苹果系统之安装Python3.6.x环境

    一.打开终端 输入:uname -a  ,查看电脑系统位数. 输入:python,查看mac系统python版本. 二.为了以后切换版本方便,安装pyenv进行版本切换以及升级. 参考文章:<M ...

  10. JS Map对象

    java和C#等高级语言中都有map这样的键值对,但是js里没有,我们需要这样的,该怎么做呢? 可以自己使用function封装一个map对象,如下所示 function Map() { this.k ...