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 ...
随机推荐
- T-SQL还有个内置方法NULLIF()
declare @cypic varchar if (NULLIF(@cypic, '') IS NOT NULL) begin print 1 end else begin print 2 end ...
- mongoDB最新版安装
转载自:http://www.higis.org/2012/04/25/ubuntu-install-mongodb/ ubuntu上安装mongodb本可以直接通过sudo apt-get inst ...
- SQL索引基础
原文发布时间为:2011-02-19 -- 来源于本人的百度文章 [由搬家工具导入] 一、深入浅出理解索引结构 实际上,您可以把索引理解为一种特殊的目录。微软的SQL SERVER提供了两种索 ...
- jquery.transform
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password: YES)
ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password: NO) ERROR 1045 (28000 ...
- MMU介绍【转】
转自:http://blog.csdn.net/martree/article/details/3321578 虚拟存储器的基本思想是程序,数据,堆栈的总的大小可以超过物理存储器的大小,操作系统把当前 ...
- jenkins 中 violation使用pylint
在jenkins中无法打开源码问题: 1. 在 Report Violations的 Source encoding 设置为 项目文件的编码, 如: utf-8. 缺省是 default. 2. 在 ...
- js遍历函数
function each(arr, callback, thisp) { if (arr.forEach) {arr.forEach(callback, thisp);} else { for (v ...
- JWT在PHP使用及问题处理
官网 https://jwt.io/ 3.0版本 https://github.com/lcobucci/jwt 安装 composer require lcobucci/jwt 依赖 PHP 5.5 ...
- ef core 2.1 利用Query Type查询视图
ef core新加入的功能“Query Type”可以让我们很方便的查询视图而不需要做任何特殊处理.不过在代码上和普通的查询有些不同. 先贴文档:https://docs.microsoft.com/ ...