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. 大数据技术 —— MapReduce 简介

    本文为senlie原创,转载请保留此地址:http://www.cnblogs.com/senlie/ 1.概要很多计算在概念上很直观,但由于输入数据很大,为了能在合理的时间内完成,这些计算必须分布在 ...

  2. AjaxFileUpload 在C#中应用

    一.前台页面 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head ru ...

  3. 常用到的Tomcat的修改方法

    1.修改端口号 打开tomcat的service.xml文件: 找到<Connector connectionTimeout="20000" port="8080& ...

  4. dorado listener属性

    每一个控件都有一个listener属性,可以用来定位一个服务定位表达式,通过这个表达式, 它最终可以映射为spring里面一个javaBean的一个java方法 例如设置DynaView1.view. ...

  5. Poj 2707 Copier Reduction

    1.Link: http://poj.org/problem?id=2707 2.Content: Copier Reduction Time Limit: 1000MS   Memory Limit ...

  6. 7.JAVA_SE复习(文件)

    文件和流 1.什么是节点流和处理流 InputStream & OutputStream Reader & Writer 乃节点流, 前面加File之类的名词 的节点流 其余加动词的均 ...

  7. 【原】Oracle拼接字段

    select FLIGHT_DATE, replace(wm_concat(FLIGHT_NO), ',', '*') FLIGHT_NO from T2001 group by FLIGHT_DAT ...

  8. [OpenXml] Generate excel in memory and dump to file

    public static void GenerateExcelFromStream() { using (MemoryStream memoryStream = new MemoryStream() ...

  9. DrawWindowFrame

    extern void DrawWindowFrame(HWND hWnd)//画窗口边框 { RECT rc; HWND DeskHwnd = ::GetDesktopWindow(); //取得桌 ...

  10. Mac下使用sublime text 2开发Python

    入门虽易, 熟练不易, 且行且珍惜 简介:这只是简单介绍一个好的文本工具sublime text的使用,如果要获得详细的教程可以去看这本书<Sublime Productivity>和一些 ...