import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List; /**
* https://leetcode.com/problems/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's Binary Tree Serialization:
*
* The serialization of a binary tree follows a level order traversal, where '#' signifies
* a path terminator where no node exists below.
*
* Here's an example:
*
* 1
* / \
* 2 3
* /
* 4
* \
* 5
*
* The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".
*/
public class UniqueBinarySearchTree2 { /**
* 找出所有唯一的二叉搜索树
*
* @param n
* @return
*/
public List<List<Character>> generateTree (int n) {
List<TreeNode> list = recursion(1, n);
List<List<Character>> result = new ArrayList<List<Character>>();
for (int i = 0; i < list.size(); i++) {
List<Character> chs = new ArrayList<Character>();
binarySearchTreeToArray(list.get(i), chs);
result.add(chs);
}
return result;
} /**
* 求出根节点分别为min-max任意一个数的时候的所有二叉搜索树
* 当根节点为i,根节点已知的情况下,一棵二叉搜索树可能的情况等于左子树可能个数乘以右子树可能个数
* 先递归求出左右子树的个数,然后根据所有左右子树的情况构造出左右的根节点,这些根节点就是最后所有可能的二叉搜索树的根节点
*
* 递归结束的条件min>max
*
* @param min
* @param max
* @return
*/
public List<TreeNode> recursion (int min, int max) {
List<TreeNode> list = new ArrayList<TreeNode>();
if (min > max) {
list.add(null);
return list;
}
for (int i = min; i <= max; i++) {
List<TreeNode> leftNodes = recursion(min, i-1);
List<TreeNode> rightNodes = recursion(i+1, max);
for (int j = 0; j < leftNodes.size(); j++) {
for (int k = 0; k < rightNodes.size(); k++) {
TreeNode root = new TreeNode(i);
root.leftChild = leftNodes.get(j);
root.rightChild = rightNodes.get(k);
list.add(root);
}
}
}
return list;
} /**
* 使用广度优先遍历将数转化为数组
*
* @param root
* @param chs
*/
public void binarySearchTreeToArray (TreeNode root, List<Character> chs) {
if (root == null) {
chs.add('#');
return;
}
List<TreeNode> list = new ArrayList<TreeNode>();
int head = 0;
int tail = 0;
list.add(root);
chs.add((char) (root.value + '0'));
tail ++;
TreeNode temp = null; while (head < tail) {
temp = list.get(head);
if (temp.leftChild != null) {
list.add(temp.leftChild);
chs.add((char) (temp.leftChild.value + '0'));
tail ++;
} else {
chs.add('#');
}
if (temp.rightChild != null) {
list.add(temp.rightChild);
chs.add((char)(temp.rightChild.value + '0'));
tail ++;
} else {
chs.add('#');
}
head ++;
}
//去除最后不必要的
for (int i = chs.size()-1; i > 0; i--) {
if (chs.get(i) != '#') {
break;
}
chs.remove(i);
}
} private class TreeNode {
TreeNode leftChild;
TreeNode rightChild;
int value; public TreeNode(int value) {
this.value = value;
} public TreeNode() {
}
} public static void print (List<List<Character>> lists) {
for (int i = 0; i < lists.size(); i++) {
System.out.println(Arrays.toString(lists.get(i).toArray(new Character[lists.get(i).size()])));
} System.out.println();
} public static void main(String[] args) {
UniqueBinarySearchTree2 uniqueBinarySearchTree2 = new UniqueBinarySearchTree2();
print(uniqueBinarySearchTree2.generateTree(0));
print(uniqueBinarySearchTree2.generateTree(1));
print(uniqueBinarySearchTree2.generateTree(2));
print(uniqueBinarySearchTree2.generateTree(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

    题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...

  4. LeetCode——Unique Binary Search Trees II

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

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

    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 dfs 深度搜索

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

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

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

  8. LeetCode:Unique Binary Search Trees I II

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

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

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

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

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

随机推荐

  1. springboot添加多数据源连接池并配置Mybatis

    springboot添加多数据源连接池并配置Mybatis 转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9190226.html May 12, 2018  ...

  2. Java是值传递还是引用传递?

    Java的值传递和引用传递在面试中一般都会都被涉及到,今天我们就来聊聊这个问题.这个问题一般是相对函数而言的,也就是Java中所说的方法参数,那么我们先来回顾一下在程序设计语言中有关参数传递给方法的两 ...

  3. Flume+Kafka+Storm整合

    Flume+Kafka+Storm整合 1. 需求: 有一个客户端Client可以产生日志信息,我们需要通过Flume获取日志信息,再把该日志信息放入到Kafka的一个Topic:flume-to-k ...

  4. 限制输入字数JS

    <tr> <th><b>说明内容:</b><span id="content">(500字以内)</span> ...

  5. 自行搭建私有云kodexplorer

    kodexplorer是一款开源的私有云框架,可以通过它实现个人网盘的功能,如果拥有一个性能不错的VPS,那么就可以摆脱奇慢无比的百度云等网盘啦!最近百度网盘还发出申明,说要限制使用空间.用别人的东西 ...

  6. React的类型检测PropTypes

    React.propTypes:React.PropTypes 提供很多验证器来验证传入数据的有效性,当向props传入无效数据时,JavaScript 控制台会抛出警告. ; class MyTit ...

  7. 4.再来看看逆向——OD的简介

    目录 1.前言 2.一些设置和配置 3.开始了解OD 代码窗口 数据窗口 小端序问题 前言 前3节主要写了恶意代码用到的手段,接下来先写一下关于逆向调试的一些内容.毕竟逆向比较难理解一点. 一些配置和 ...

  8. Spring Cloud 组件 —— feign

    feign 作为一个声明式的 Http Client 开源项目.在微服务领域,相比于传统的 apache httpclient 与在 spring 中较为活跃的 RestTemplate 更面向服务化 ...

  9. EF的简单认识

    EF的简单认识   EF简介 EntityFramwork是微软提供的一款ORM框架(Object Relational Mapping),实体映射模型,它的底层是ADO.NET的机制,使用EF将省去 ...

  10. AI-逻辑回归函数、激活函数、损失函数

    最近开始学习人工智能,先从基本的概念学起 逻辑回归函数(预测函数):z = dot(w,x) + b 解释:假设有三个特征,即x可以表示为(x1,x2,x3),w表示权重,对应每个特征的重要程度,b表 ...