leetCode 95.Unique Binary Search Trees II (唯一二叉搜索树) 解题思路和方法
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 (唯一二叉搜索树) 解题思路和方法的更多相关文章
- [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 ...
- [LeetCode] 95. Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- [Leetcode] Unique binary search trees ii 唯一二叉搜索树
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- LeetCode OJ:Unique Binary Search Trees(唯一二叉搜索树)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 【LeetCode-面试算法经典-Java实现】【096-Unique Binary Search Trees(唯一二叉搜索树)】
[096-Unique Binary Search Trees(唯一二叉搜索树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given n, how many s ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- [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 ...
- 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 ...
随机推荐
- 从jekyll转向hexo
当年选择jekyll是因为看中了HCZ Material theme这个主题,折腾了很久才把博客搭建好,后来周边人准备些博客的时候已经不推荐使用jekyll了,推荐hexo给好几个人,不用他们折腾,( ...
- photoshop 安装
Photoshop 下载: http://www.duote.com/soft/54352.html 下载完后解压选择..\Adobe CS6\Set-up.exe ,点击 Set-up.exe ...
- 问题:viewController不会调用dealloc()不会销毁
问题 在调试程序时,我从ViewController A push进 ViewController B,在从B back时发现程序不会执行B里面的dealloc(),很诡异的问题,因为按理说此时点击b ...
- 讲的很详细的一篇关于object equals() & hashCode() 的文章
转: 讲的很详细的一篇关于object equals() & hashCode() 的文章 哈希表这个数据结构想必大多数人都不陌生,而且在很多地方都会利用到hash表来提高查找效率.在Java ...
- pat 团体天梯赛 L1-039. 古风排版
L1-039. 古风排版 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 中国的古人写文字,是从右向左竖向排版的.本题就请你编写 ...
- 访问外网 ML2 的配置
通过 router 可以实现位于不同 vlan 中的 instance 之间的通信. 接下来要探讨的问题是 instance 如何与外部网络通信. 这里的外部网络是指的租户网络以外的网络. 租户网络是 ...
- Jquery : 上下滚动--单行 批量多行 文字图片翻屏【转】
原文发布时间为:2010-02-01 -- 来源于本人的百度文章 [由搬家工具导入] 注:如果和左右滚动一起使用,则会出现冲突 一单行滚动(ad:http://www.gz138.com) <! ...
- AspNetPager分页控件官方网站
原文发布时间为:2009-12-04 -- 来源于本人的百度文章 [由搬家工具导入] http://www.webdiyer.com/AspNetPager
- 从头实现一个koa框架
koajs是最流行的nodejs后端框架之一,有很多网站都使用koa进行开发,同时社区也涌现出了一大批基于koa封装的企业级框架.然而,在这些亮眼的成绩背后,作为核心引擎的koa代码库本身,却非常的精 ...
- Google的新一代一致性哈希算法
先看到了一篇中文文章:谷歌退出有界负载的一致性哈希算法 然后找到了它对应的英文原文 以及它的英文paper,24页 又找到一篇博客,发现已经有人将它在haproxy上实现了,haproxy1.7.5上 ...