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

confused what "{1,#,2,3}" means? >
read more on how binary tree is serialized on OJ.

思路:这一题尽管比較繁琐。可是难度不算非常大,能够运用排列组合的思想,全排列,然后查找符合要求的二叉搜索树就可以。

也能够运用递归,将数分为左右子树,进而简化求解。

排列组合思想代码例如以下(不知为什么OJ未通过,n=2时报错。但本地測试全然正确):

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
boolean[] b;
List<TreeNode> list;
Set<String> set = new HashSet<String>();
public List<TreeNode> generateTrees(int n) {
b = new boolean[n];
int[] a = new int[n];
list = new ArrayList<TreeNode>();
for(int i = 0; i < n; i++){
a[i] = i+1;
}
create(a,null,n);
return list;
} /**
* 生成二叉搜索树
*/
private void create(int[] a,TreeNode root,int nn){
if(nn == 0){
TreeNode q = root;
String s = preOrder(q, "");
//System.out.println(s);
if(set.add(s)){
list.add(root);
}
return;
} for(int i = 0; i < a.length; i++){
if(!b[i]){
b[i] = true;
TreeNode p = new TreeNode(a[i]);
root = insert(root,p);
create(a,root,nn-1);
root = delete(root,p);
b[i] = false;
}
}
} /**
* 前序遍历
* @param root
* @param s
* @return
*/
private String preOrder(TreeNode root,String s){
if(root != null){
s += root.val; if(root.left != null){
s = preOrder(root.left, s);
} if(root.right != null){
s = preOrder(root.right, s);
}
}
return s;
} /**
* 删除节点
* @param root
* @param p
* @return
*/
private TreeNode delete(TreeNode root, TreeNode p) {
if(root.val == p.val)
return null;
if(root.val < p.val){
root.right = delete(root.right,p);
}else{
root.left = delete(root.left,p);
}
return root;
} /**
* 将新节点插入二叉搜索树
*/
private TreeNode insert(TreeNode root,TreeNode node){
TreeNode p = root; if(p == null){
p = node;
return p;
}
if(node.val < p.val){
root.left = insert(p.left,node);
}else{
root.right = insert(p.right,node);
}
return root;
}
}

递归解法:

/**
* 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){
List<TreeNode> list = new ArrayList<TreeNode>();
if(n <= 0){
list.add(null);
return list;
}
list = createTree(1,n); return list;
}
/**
* 循环生产二叉搜索树
* @param i 開始值
* @param j 结束值
* @return
*/
private List<TreeNode> createTree(int i, int j){ List<TreeNode> list = new ArrayList<TreeNode>();
//起始大于结束值,加入null
if(i > j){
list.add(null);
return list;
}
//相等也即加入一个
if(i == j){
list.add(new TreeNode(i));
return list;
}
//循环加入
for(int k = i; k <= j; k++){
//左子树肯定比i小
List<TreeNode> left = createTree(i,k-1);
//右子树肯定比i大
List<TreeNode> right = createTree(k+1,j);
//将结果循环加入
for(TreeNode l:left){
for(TreeNode r:right){
TreeNode root = new TreeNode(k);
root.left = l;
root.right = r;
list.add(root);
}
}
}
return list;
}
}

leetCode 95.Unique Binary Search Trees II (唯一二叉搜索树) 解题思路和方法的更多相关文章

  1. [LeetCode] 95. Unique Binary Search Trees II 唯一二叉搜索树 II

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

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

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

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

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

  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 OJ:Unique Binary Search Trees(唯一二叉搜索树)

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  6. 【LeetCode-面试算法经典-Java实现】【096-Unique Binary Search Trees(唯一二叉搜索树)】

    [096-Unique Binary Search Trees(唯一二叉搜索树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given n, how many s ...

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

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

  8. [leetcode]95. Unique Binary Search Trees II给定节点形成不同BST的集合

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

  9. leetcode 95 Unique Binary Search Trees II ----- java

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

随机推荐

  1. 各种 Python 实现的简单介绍与比较

    当谈到Python时,一般指的是CPython.但Python实际上是一门语言规范,只是定义了Python这门语言应该具备哪些语言要素,应当能完成什么样的任务.这种语言规范可以用不同的方式实现,可以用 ...

  2. HTML 改变文字方向

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. EclEmma Java Code Coverage for Eclipse

      1. 1EclEmma的介绍 一.EclEmma 简介: EclEmma是一个开源的软件测试工具(for eclipse),可以在编码过程中查看代码调用情况.也可以检测单覆盖率. 详见: http ...

  4. 【CF1043C】Smallest Word(构造)

    题意:给定一个由a和b构成的字符串,可以选择翻转或不翻转他的每个前缀,翻转记为1不翻转记为0,求能将字符串排序的字典序最小的操作序列 n<=1e3 思路:考虑极长的一段a [t,w] 翻转t-1 ...

  5. ext2/3/4的inode结构说明

    系统环境:Ubuntu15.10/ext4 今天在复习<鸟哥的私房菜-基础学习篇>,看到inode大小为128bytes,想看下这128字节里面到底是什么样的. 于是我查了下google, ...

  6. UVALive 3664:Guess(贪心 Grade E)

    vj题目链接 题意: 有n (n<16345)个人,每个人有三个数(小于1000且最多两位小数点),表示答对对应题的得分.规定总分越高的人rank越高.总分相同,id小的rank高.现在知道ra ...

  7. 洛谷——P2383 狗哥玩木棒

    P2383 狗哥玩木棒 题目背景 狗哥又趁着语文课干些无聊的事了... 题目描述 现给出一些木棒长度,那么狗哥能否用给出的木棒(木棒全用完)组成一个正方形呢? 输入输出格式 输入格式: 输入文件中的第 ...

  8. POJ 3480 John [博弈之Nim 与 Anti-Nim]

    Nim游戏:有n堆石子,每堆个数不一,两人依次捡石子,每次只能从一堆中至少捡一个.捡走最后一个石子胜. 先手胜负:将所有堆的石子数进行异或(xor),最后值为0则先手输,否则先手胜. ======== ...

  9. luogu P1260 工程规划(luogu wa)don't know way

    题目描述 造一幢大楼是一项艰巨的工程,它是由n个子任务构成的,给它们分别编号1,2,…,n(5≤n≤1000).由于对一些任务的起始条件有着严格的限制,所以每个任务的起始时间T1,T2,…,Tn并不是 ...

  10. HDU - 2970 Suffix reconstruction

    Discription Given a text s[1..n] of length n, we create its suffix array by taking all its suffixes: ...