Skip to content

Commit 463174d

Browse files
committed
fd
1 parent 02457a0 commit 463174d

File tree

2 files changed

+112
-25
lines changed

2 files changed

+112
-25
lines changed

leetcode/solution/src/UniqueBinarySearchTreesII.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,84 @@ private List<TreeNode> generateTrees(int start, int end) {
3636
}
3737
return list;
3838
}
39+
40+
/**
41+
* 这里面明显有重复计算的部分,可缓存[1,n]的结果加速,时间降到2ms
42+
*/
43+
/*
44+
public List<TreeNode> generateTrees(int start, int end, List<TreeNode>[] map) {
45+
List<TreeNode> result = new LinkedList<>();
46+
if (start > end) {
47+
result.add(null);
48+
return result;
49+
}
50+
if (start == 1 && map[end] != null) {
51+
return map[end];
52+
}
53+
for (int i = start; i <= end; i++) {
54+
List<TreeNode> lefts = generateTrees(start, i - 1, map);
55+
List<TreeNode> rights = generateTrees(i + 1, end, map);
56+
for (TreeNode left : lefts) {
57+
for (TreeNode right : rights) {
58+
TreeNode root = new TreeNode(i);
59+
root.left = left;
60+
root.right = right;
61+
result.add(root);
62+
}
63+
}
64+
}
65+
if (start == 1) {
66+
map[end] = result;
67+
}
68+
return result;
69+
}
70+
71+
public List<TreeNode> generateTrees(int n) {
72+
if (n < 1) {
73+
return Collections.EMPTY_LIST;
74+
}
75+
List<TreeNode>[] map = new LinkedList[n + 1];
76+
return generateTrees(1, n, map);
77+
}*/
78+
79+
/**
80+
* 可考虑迭代的DP写法
81+
*/
82+
/*
83+
public List<TreeNode> generateTrees(int n) {
84+
if (n == 0) {
85+
return Collections.EMPTY_LIST;
86+
}
87+
88+
List<TreeNode>[] map = new LinkedList[n + 1];
89+
map[0] = new LinkedList<>();
90+
map[0].add(null);
91+
92+
for (int i = 1; i <= n; i++) {
93+
map[i] = new LinkedList<>();
94+
for (int j = 1; j <= i; j++) {
95+
for (TreeNode left : map[j - 1]) {
96+
for (TreeNode right : map[i - j]) {
97+
TreeNode root = new TreeNode(j);
98+
root.left = left;
99+
root.right = clone(right, j);
100+
map[i].add(root);
101+
}
102+
}
103+
}
104+
}
105+
106+
return map[n];
107+
}
108+
109+
public TreeNode clone(TreeNode node, int offset) {
110+
if (node == null) {
111+
return null;
112+
}
113+
TreeNode root = new TreeNode(node.val + offset);
114+
root.left = clone(node.left, offset);
115+
root.right = clone(node.right, offset);
116+
return root;
117+
}
118+
*/
39119
}

leetcode/src/Main.java

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,43 @@
44

55
public class Main {
66

7-
public static void flatten(TreeNode root) {
8-
Stack<TreeNode> stack = new Stack<TreeNode>();
9-
List<TreeNode> list = new ArrayList<>();
10-
while (!stack.isEmpty() || root != null) {
11-
if (root != null) {
12-
list.add(root);
13-
stack.push(root);
14-
root = root.left;
15-
} else {
16-
root = stack.pop().right;
7+
public List<TreeNode> generateTrees(int n) {
8+
if (n == 0) {
9+
return Collections.EMPTY_LIST;
10+
}
11+
12+
List<TreeNode>[] map = new LinkedList[n + 1];
13+
map[0] = new LinkedList<>();
14+
map[0].add(null);
15+
16+
for (int i = 1; i <= n; i++) {
17+
map[i] = new LinkedList<>();
18+
for (int j = 1; j <= i; j++) {
19+
for (TreeNode left : map[j - 1]) {
20+
for (TreeNode right : map[i - j]) {
21+
TreeNode root = new TreeNode(j);
22+
root.left = left;
23+
root.right = clone(right, j);
24+
map[i].add(root);
25+
}
26+
}
1727
}
1828
}
19-
for (int i = 0; i < list.size() - 1; i++) {
20-
list.get(i).left = null;
21-
list.get(i).right = list.get(i + 1);
29+
30+
return map[n];
31+
}
32+
33+
public TreeNode clone(TreeNode node, int offset) {
34+
if (node == null) {
35+
return null;
2236
}
37+
TreeNode root = new TreeNode(node.val + offset);
38+
root.left = clone(node.left, offset);
39+
root.right = clone(node.right, offset);
40+
return root;
2341
}
2442

2543
public static void main(String[] args) {
26-
TreeNode node3 = new TreeNode(3);
27-
TreeNode node4 = new TreeNode(4);
28-
TreeNode node2 = new TreeNode(2, node3, node4);
29-
TreeNode node6 = new TreeNode(6);
30-
TreeNode node5 = new TreeNode(5, null, node6);
31-
TreeNode node1 = new TreeNode(1, node2, node5);
32-
flatten(node1);
33-
34-
while (node1 != null) {
35-
System.out.print(node1.val + " ");
36-
node1 = node1.right;
37-
}
44+
3845
}
3946
}

0 commit comments

Comments
 (0)