-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP0095_UniqueBinarySearchTreesII.java
More file actions
75 lines (69 loc) · 2.47 KB
/
P0095_UniqueBinarySearchTreesII.java
File metadata and controls
75 lines (69 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package yyl.leetcode.p00;
import java.util.ArrayList;
import java.util.List;
import yyl.leetcode.bean.TreeNode;
/**
* <h3>不同的二叉搜索树 II</h3><br>
* 给定一个整数 n,生成所有由 1 ... n 为节点所组成的“二叉搜索树”。<br>
*
* <pre>
* 示例:
* 输入: 3
* 输出:
* [
* [1,null,3,2],
* [3,2,null,1],
* [3,1,null,null,2],
* [2,1,3],
* [1,null,2,null,3]
* ]
* 解释:
* 以上的输出对应以下 5种不同结构的二叉搜索树:
* 1 3 3 2 1
* \ / / / \ \
* 3 2 1 1 3 2
* / / \ \
* 2 1 2 3
* </pre>
*/
public class P0095_UniqueBinarySearchTreesII {
public static void main(String[] args) {
Solution solution = new Solution();
List<TreeNode> trees = solution.generateTrees(3);
for (TreeNode tree : trees) {
System.out.println(tree);
}
}
// 递归求解
// 题目要求是“二叉搜索树”,二叉搜索树的特性是:左节点小于根节点,右节点大于根节点。
// 从序列 1...n 中取出数字 i,作为当前树的树根。于是,剩余 i - 1 个元素可用于左子树,n - i 个元素用于右子树。
// 每次会产生 G(i-1)种左子树 和 G(n-i)种右子树
static class Solution {
public List<TreeNode> generateTrees(int n) {
if (n <= 0) {
return new ArrayList<>();
}
return generateTrees(1, n);
}
private List<TreeNode> generateTrees(int start, int end) {
List<TreeNode> result = new ArrayList<>();
if (start > end) {
result.add(null);
} else {
for (int i = start; i <= end; ++i) {
List<TreeNode> left = generateTrees(start, i - 1);
List<TreeNode> right = generateTrees(i + 1, end);
for (int j = 0; j < left.size(); ++j) {
for (int k = 0; k < right.size(); ++k) {
TreeNode root = new TreeNode(i);
root.left = left.get(j);
root.right = right.get(k);
result.add(root);
}
}
}
}
return result;
}
}
}