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));
}
}
``