题目:

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

思路:

仍和Unique Binary Search Trees的思路一样,确定左右树。

package bst;

import java.util.ArrayList;
import java.util.List; class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
} public class UniqueBinarySearchTreesII { public List<TreeNode> generateTrees(int n) {
List<TreeNode> res = new ArrayList<TreeNode>();
if (n <= 0) return res;
return generateTrees(1, n);
} private List<TreeNode> generateTrees(int left, int right) {
List<TreeNode> roots = new ArrayList<TreeNode>();
if (left > right || left <= 0 || right <= 0) {
roots.add(null);
} else if (left == right) {
TreeNode root = new TreeNode(left);
roots.add(root);
} else {
for (int i = left; i <= right; ++i) {
List<TreeNode> leftTree = generateTrees(left, i - 1);
List<TreeNode> rightTree = generateTrees(i + 1, right);
for (int j = 0; j < leftTree.size(); ++j) {
for (int k = 0; k < rightTree.size(); ++k) {
TreeNode root = new TreeNode(i);
root.left = leftTree.get(j);
root.right = rightTree.get(k);
roots.add(root);
}
}
}
} return roots;
} public static void main(String[] args) {
// TODO Auto-generated method stub
UniqueBinarySearchTreesII u = new UniqueBinarySearchTreesII();
u.generateTrees(3);
} }

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

  1. LeetCode: 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 独一无二的二叉搜索树之二

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

  3. LeetCode——Unique Binary Search Trees II

    Question Given an integer n, generate all structurally unique BST's (binary search trees) that store ...

  4. [Leetcode] Unique binary search trees ii 唯一二叉搜索树

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

  5. [LeetCode] Unique Binary Search Trees II dfs 深度搜索

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

  6. [leetcode]Unique Binary Search Trees II @ Python

    原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees-ii/ 题意:接上一题,这题要求返回的是所有符合条件的二叉查找树,而上 ...

  7. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

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

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

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

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

随机推荐

  1. Codeforces Round #382 (Div. 2)C. Tennis Championship 动态规划

    C. Tennis Championship 题目链接 http://codeforces.com/contest/735/problem/C 题面 Famous Brazil city Rio de ...

  2. 当创业遇上O2O,新一批死亡名单,看完震惊了!

    当创业遇上O2O,故事就开始了,总投入1.6亿.半年开7家便利店.会员猛增至10万……2015半年过去后,很多故事在后面变成了一场创业“事故”,是模式错误还是烧钱过度?这些项目的失败能给国内创业者带来 ...

  3. 用etckeeper来解救运维工程师

    对于运维工程师来讲,etc环境是一个痛点,各种配置,各种修改,某些软件的配置关联因素过多的话,那就更加痛苦了,改完发现不对再想改回去都千难万难, 现在有一个好的解决方案,那就是用etckeeper,绝 ...

  4. WPF总结

    WPF UI布局 模板总结控件可以通过ItemTemplate="{StaticResource Template}"绑定指定的模板: 数据源总结控件可以通过ItemsSource ...

  5. 解读Gartner《2015年度新兴技术成熟度曲线报告》

    详细见:http://www.360doc.com/content/16/0209/16/26186435_533443133.shtml 今年的报告评估了112个领域超过2000项新型技术的市场类型 ...

  6. github 使用记录

    安装客户端tortoiseGit 是服务端,要想在自己电脑上使用git我们还需要一个git客户端,我这里选用TortoiseGit,他给我们提供了图形界面的操作.在安装之前首先需要安装git,下载地址 ...

  7. 修改windows系統下xampp中apache端口被其他程式占用的問題

    windows 7安裝後啟動xampp, 提示port 443 被其他程式占用. 網上查找解決方案: http://stackoverflow.com/questions/21182512/how-t ...

  8. Singleton<T>

    代码如下: public class Singleton<T> where T : class { private static T _instance; private static r ...

  9. IT Operations(IT 运营),运维的更价值化认识

    一直想努力向别人(甚至包括从事运维的人)解释清楚什么是运维,发现很难! 6月20号,在InfoQ高效运维群里面,对运维创业做了一次激烈的讨论,很自然地,过程中不可避免的谈到运维苦逼和运维无法产品化的问 ...

  10. VirtualBox中安装CentOS-6.6虚拟机

    1. 下载 可以到官网下载,http://mirror.centos.org/centos/ 如果下载速度太慢的话,也可以到163镜像下载: http://mirrors.163.com/centos ...