首页 技术 正文
技术 2022年11月15日
0 收藏 663 点赞 4,385 浏览 2919 个字
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));
}
}
``
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,821
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905