-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUniqueBST.java
58 lines (50 loc) · 1.47 KB
/
UniqueBST.java
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
package com.crossover.demo;
import java.util.LinkedList;
import java.util.List;
public class UniqueBST {
static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
left = null;
right = null;
}
}
public static void main(String[] args) {
}
public static int numTrees(int n) {
int arr[] = new int[n + 1];
arr[0] = 1;
for (int i = 0; i <= n; i++) {
for (int j = 0; j < i; j++) {
arr[i] += arr[j] * arr[i - j - 1];
}
}
return arr[n];
}
public static List<TreeNode> generateTrees(int start, int end) {
List<TreeNode> list = new LinkedList<>();
if (start > end) {
list.add(null);
return list;
}
for (int i = start; i <= end; i++) {
List<TreeNode> lefts = generateTrees(start, i - 1);
List<TreeNode> rights = generateTrees(i + 1, end);
for (TreeNode left : lefts) {
for (TreeNode right : rights) {
TreeNode root = new TreeNode(i);
root.left = left;
root.right = right;
list.add(root);
}
}
}
return list;
}
public List<TreeNode> generateTrees(int n) {
return generateTrees(1, n);
}
}