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
 /**
* 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 {
public ArrayList<TreeNode> generateTrees(int n) {
// Start typing your Java solution below
// DO NOT write main() function
return generateTrees(1,n);
}
public ArrayList<TreeNode> generateTrees(int a, int b){
ArrayList<TreeNode> res = new ArrayList<TreeNode>();
if(a>b) res.add(null);
for(int i=a;i<=b;i++){
ArrayList<TreeNode> temp1 = generateTrees(a,i-1);
ArrayList<TreeNode> temp2 = generateTrees(i+1,b);
for(TreeNode n:temp1)
for(TreeNode m:temp2){
TreeNode temp= new TreeNode(i);
temp.left=n;
temp.right=m;
res.add(temp);
}
}
return res;
}
}

Unique Binary Search Trees II的更多相关文章

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

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

  2. 【leetcode】Unique Binary Search Trees II

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

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

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

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

  5. Unique Binary Search Trees,Unique Binary Search Trees II

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

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

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

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

  8. 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]* ...

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

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

  10. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

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

随机推荐

  1. CSS选择器介绍

    一.元素选择器 E{...} 二.属性选择器 E[attr]{...}:指定该CSS对具有attr的元素起作用: E[attr=value]{...}::指定该CSS对具有attr的值为value的元 ...

  2. 服务器发布WebService返回DataTable

    初始化Datatable时,需要为Datatable命名.否则在客户端使用时,会报“datatable不能序列化...”导致表格无法从服务器端读取到. 例如: 服务器端: DataTable dt = ...

  3. 利用Echarts设计一个图表平台(一)

    Echarts是一款百度的开源图表库,里面提供了非常多的图表样式,我们今天要讲的内容是利用这一款开源js图表,制作一个能够动态定制的图表平台. 1)Echarts API介绍 首先我们先来看一下Ech ...

  4. Linux 下cronolog分割catalina.out文件

    开发项目的时候查看日志,发现catalina.out已经有1个多G,日积月累的慢慢变大,幸亏及时发现还没有导致错误, tomcat默认日志之一输出在catalina.out文件中的,不会分割,不便于使 ...

  5. DDL_数据库模式定义语言

    2014年11月22日 15:53:24 DDL 定义  define 概念:是用于描述数据库中要存储的现实世界实体的语言.一个数据库模式包含该数据库中所有实体的描述定义.               ...

  6. 滑动选择日期(基于sui-mobile的移动端)

    $(page).on('touchmove','#touchMoveTime',function (event) { touchMove(event); }); scrollBarInit(); // ...

  7. 手机网站中 限制图片宽度 JS图片等比例缩放

    <script type="text/javascript"> $(function () { var w = $(".content-co").w ...

  8. Cocos2d-x第一个坑,NDK 编译环境

    这些天搭建windows cocos2d-x的环境,基本上崩溃到死.目前好转.终于可以编译通过: 生成模板工程:在cmd下进入cocos2d-x的主目录,D:\Android\cocos2d-x-2. ...

  9. ubuntu 安装dell无线网卡2

    以下转自:http://blog.sina.com.cn/s/blog_73b6331101016haq.html ubuntu 12.04 bcm43xx无线网卡安装记录 (2012-07-01 0 ...

  10. poj 3415 Common Substrings 后缀数组+单调栈

    题目链接 题意:求解两个字符串长度 大于等于k的所有相同子串对有多少个,子串可以相同,只要位置不同即可:两个字符串的长度不超过1e5; 如 s1 = "xx" 和 s2 = &qu ...