Question

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 

Solution

Key to the solution is to divide the problem into two sub-problems. Unlike "Unique Binary Search Trees", this problem is NP-hard.

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<TreeNode> generateTrees(int n) {
return generateTreesHelper(1, n);
}
private List<TreeNode> generateTreesHelper(int start, int end) {
List<TreeNode> result = new ArrayList<TreeNode>();
if (start > end) {
result.add(null);
return result;
}
for (int i = start; i <= end; i++) {
List<TreeNode> lefts = generateTreesHelper(start, i - 1);
List<TreeNode> rights = generateTreesHelper(i + 1, end);
for (TreeNode left : lefts) {
for (TreeNode right : rights) {
TreeNode tmp = new TreeNode(i);
tmp.left = left;
tmp.right = right;
result.add(tmp);
}
}
}
return result;
}
}

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

随机推荐

  1. LCS算法思想

    LCS问题就是求两个字符串最长公共子串的问题.解法就是用一个矩阵来记录两个字符串中所有位置的两个字符之间的匹配情况,若是匹配则为1,否则为0.然后求出对角线最长的1序列,其对应的位置就是最长匹配子串的 ...

  2. 怎样合并排序数组(How to merge 2 sorted arrays?)

    Question: We have 2 sorted arrays and we want to combine them into a single sorted array. Input: arr ...

  3. hdu4267 A Simple Problem with Integers

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (J ...

  4. android CMWAP, CMNET有何差别

    什么是CMNET,什么是CMWAP? 答:CMWAP和CMNET仅仅是中国移动为其划分的两个GPRS接入方式.中国移动对CMWAP作了一定的限制,主要表如今CMWAP接入时仅仅能訪问GPRS网络内的I ...

  5. Android窗口管理服务WindowManagerService对窗口的组织方式分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8498908 我们知道,在Android系统中, ...

  6. NET基础课--泛型(NET之美)

    1.泛型,类型或方法的一种抽象概括. 2.泛型类:在类型名后面加一个<>,其中传递占位符,也就是类型参数.where是类型约束 可以再查资料 public class SortHelper ...

  7. ASP.NET的Application简介1

    ASP.NET中的Application 1. Application是用于保存所有用户共有的信息.在ASP时代,如果要保存的数据在应用程序生存期内不会或者很少改变,那么使用Application是理 ...

  8. System.Speech.Synthesis 添加暂停、继续功能

    为了方便调用暂停.继续的方法.要将speech的功能写成一个类.直接附上代码: using System; using System.Collections.Generic; using System ...

  9. web跳转到自己的app

    做个笔记 原文:http://blog.csdn.net/ba_jie/article/details/6884818 iPhone SDK可以把你的App和一个自定义的URL Scheme绑定.该U ...

  10. JavaScript禁止用户多次提交方法

    [当服务器超载时,会出现提交卡顿的现象,但是用户在操作时,会不停重复点击提交,会造成服务器压力更大.所以我们需要进行限制] [1]将提交按钮禁止 <html> <head> & ...