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. selenium 打开新标签页(非窗口)

    如何利用webdriver打开多个标签页和链接呢,到处查找得到的往往只是如何打开标签页. 打开标签页很简单,浏览器打开标签页的快捷键是ctrl+t,那把ctrl+t的按键事件传入即可,很多种实现方式, ...

  2. 接口测试——postman & jmeter

    新名词: 自动化测试:写代码帮你测试 接口:是一个抽象的概念,一种交互关系. 抓包:拦截请求. 接口测试:就是功能测试,比后者还简单. 需要有测试文档,包括项目.模块.URL.请求方式.参数.参数说明 ...

  3. Oracle在.sql文件中创建存储过程

    创建存储过程的语法网上到处都有. 可我执行了半天都创建不成功. 最后,发现! 在最后加个 / 就可以了!!! 真坑啊 今天连续被Oracle坑了两次了. 最后,感谢这个人https://blog.cs ...

  4. [jzoj]3468.【NOIP2013模拟联考7】OSU!(osu)

    Link https://jzoj.net/senior/#main/show/3468 Description osu 是一款群众喜闻乐见的休闲软件. 我们可以把osu的规则简化与改编成以下的样子: ...

  5. React(九)create-react-app创建项目 + 按需加载Ant Design

    (1)create-react-app如何创建项目我前面第一章介绍过了,这里就不过多写了, (2)我们主要来说说按需加载的问题 1. 引入antd npm install antd --save 2. ...

  6. windows服务定时任务

    其实定时任务时不时会碰到,只不过解决方案也不是只有一个,网上也有很多文章,但是没有一篇说得很清楚,尤其是安装环节,今天就着重说一下安装, 其他步骤带过,C#开发windows服务,开发,安装,调试 1 ...

  7. Linux shell编程— 命令替换

    有两种方法可以将命令输出赋给变量 反引号字符(`) $()格式 命令替换允许你将shell 命令的输出赋给变量 要么用一对反引号把整个命令行围起来: testing=`data` 要么使用$()格式 ...

  8. win10 anaconda安装后使用报错“Original error was: DLL load failed: 找不到指定的模块”

    报错:Original error was: DLL load failed: 找不到指定的模块. 环境变量需要添加3个 然后就okay了.

  9. ucore代码分析

    lab2: 总共分为四个包一个文件,分别为: boot: 操作系统加载程序代码 kern: 操作系统内核代码 libs: 相关的库和数据结构 tools: 相关编译链接调试工具 Makefile: 构 ...

  10. pycharm 中 django 导入静态文件不提示补全

    File—>setting----->Languages & Frameworks ------> Python Template  Languages ------> ...