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. 使用state改变的jsx监听不到数据变化的问题

    当使用state来改变一个组件内部的虚拟dom的时候,该虚拟dom是无法监听到state数据的变化的,他只会绑定state改变dom当时的数据.

  2. .net显示今天农历的代码!

    原文发布时间为:2010-04-11 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Globalization;//namespace Proje ...

  3. eclipse在linux環境下安裝注意事项

    文件如果安装在非home文件夹下必须为eclipse授权 sudo chmod -R 777 /usr/tools/eclipse

  4. cobbler一键部署centos7.4(脚本)

    执行脚本之前你需要做四件事 1. 关闭防火墙 2.关闭selinux 3.配置163或者阿里云的 yum源 4.上传centos7.4的镜像如下图 [root@cobbler ~]# cat auto ...

  5. Axisfault faultcode:Server.userException异常

    ---恢复内容开始--- Axisfault faultcode:Server.userException异常 AxisFault faultCode: {http://schemas.xmlsoap ...

  6. codevs——1154 能量项链(区间DP)

    2006年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解    题目描述 Description 在Mars星球上,每个Mars人 ...

  7. luogu P3147 [USACO16OPEN]262144

    题目描述 Bessie likes downloading games to play on her cell phone, even though she doesfind the small to ...

  8. bzoj 4465: [Jsoi2013]游戏中的学问

    4465: [Jsoi2013]游戏中的学问 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 121  Solved: 59[Submit][Statu ...

  9. centos7下使用wget命令安装mysql

    1.首先安装wget命令: yum -y install  wget 2.下载mysql wget http://repo.mysql.com/mysql-community-release-el7- ...

  10. SecureCRT介绍、安装、使用(转)

    http://blog.csdn.net/liang19890820/article/details/49701429 简介 SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,简 ...