Skip to content

Commit e15dd02

Browse files
committed
commit
1 parent bad4d98 commit e15dd02

File tree

3 files changed

+67
-17
lines changed

3 files changed

+67
-17
lines changed

[096][Unique Binary Search Trees]/src/Solution.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,40 @@
77
public class Solution {
88
/**
99
* Given n, how many structurally unique BST's (binary search trees) that store values 1...n?
10-
*
10+
* <p>
1111
* For example,
1212
* Given n = 3, there are a total of 5 unique BST's.
13-
* 1 3 3 2 1
14-
* \ / / / \ \
15-
* 3 2 1 1 3 2
16-
* / / \ \
13+
* 1 3 3 2 1
14+
* \ / / / \ \
15+
* 3 2 1 1 3 2
16+
* / / \ \
1717
* 2 1 2 3
18-
*
18+
* <p>
1919
* µÝÍÆ¹«Ê½
20-
* f(k)*f(n-1-k):f(k)表示根结点左子树有k个结点,其有的形状是f(k),f(n-1-k)表示右子树有n-1-k个结点
21-
*
22-
* f(n) = 2*f(n-1) + f(1)*f(n-2) + f(2)f(n-3) + f(3)f(n-4) + ... +f(n-2)*f(1)
20+
* f(0) = 1
21+
* f(1) = 1
22+
* f(i) = f(0)f(i-1) + f(1)f(i-1) + ... + f(i-1)f(0)
2323
*
2424
* @param n
2525
* @return
2626
*/
2727
public int numTrees(int n) {
2828

2929
if (n <= 0) {
30-
return 0;
30+
return 1;
3131
} else if (n == 1) {
3232
return 1;
3333
}
3434

3535
int[] result = new int[n + 1];
36-
result[0] = 0;
36+
result[0] = 1;
3737
result[1] = 1;
3838

3939

4040
// Çóf(2)...f(n)
4141
for (int i = 2; i <= n; i++) {
42-
// 求f(i)
43-
result[i] = 2 * result[i - 1];
44-
for (int j = 1; j <= i - 1 ; j++) {
45-
result[i] += result[j]*result[i - 1 -j];
42+
for (int j = 1; j <= i; j++) {
43+
result[i] += result[j - 1] * result[i - j];
4644
}
4745

4846
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Author: 王俊超
3+
* Date: 2015-06-18
4+
* Time: 17:36
5+
* Declaration: All Rights Reserved !!!
6+
*/
7+
public class Solution2 {
8+
/**
9+
* Given n, how many structurally unique BST's (binary search trees) that store values 1...n?
10+
*
11+
* For example,
12+
* Given n = 3, there are a total of 5 unique BST's.
13+
* 1 3 3 2 1
14+
* \ / / / \ \
15+
* 3 2 1 1 3 2
16+
* / / \ \
17+
* 2 1 2 3
18+
*
19+
* 递推公式
20+
* f(k)*f(n-1-k):f(k)表示根结点左子树有k个结点,其有的形状是f(k),f(n-1-k)表示右子树有n-1-k个结点
21+
*
22+
* f(n) = 2*f(n-1) + f(1)*f(n-2) + f(2)f(n-3) + f(3)f(n-4) + ... +f(n-2)*f(1)
23+
*
24+
* @param n
25+
* @return
26+
*/
27+
public int numTrees(int n) {
28+
29+
if (n <= 0) {
30+
return 1;
31+
} else if (n == 1) {
32+
return 1;
33+
}
34+
35+
int[] result = new int[n + 1];
36+
result[0] = 0;
37+
result[1] = 1;
38+
39+
40+
// 求f(2)...f(n)
41+
for (int i = 2; i <= n; i++) {
42+
// 求f(i)
43+
result[i] = 2 * result[i - 1];
44+
for (int j = 1; j <= i - 1 ; j++) {
45+
result[i] += result[j]*result[i - 1 -j];
46+
}
47+
48+
}
49+
return result[n];
50+
}
51+
}

[106][Construct Binary Tree From Inorder And Postorder Traversal]/src/Solution.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ public class Solution {
3131
public TreeNode buildTree(int[] inorder, int[] postorder) {
3232

3333
// 参数检验
34-
if (inorder == null || postorder == null || inorder.length == 0
35-
|| inorder.length != postorder.length) {
34+
if (inorder == null || postorder == null || inorder.length == 0 || inorder.length != postorder.length) {
3635
return null;
3736
}
3837

@@ -70,13 +69,15 @@ else if (x < y) {
7069
}
7170

7271
// 左子树非空,构建左子树
72+
// [x, x+1, ..., idx-1] -> 总计 idx-x个
7373
int leftLength = idx - x;
7474
if (leftLength > 0) {
7575
// i, i + leftLength - 1,前序遍历的左子树的起始,结束位置
7676
root.left = solve(inorder, x, idx - 1, postorder, i, i + leftLength - 1);
7777
}
7878

7979
// 右子树非空,构建右子树
80+
// [idx+1, idx+2, ..., y] -> 总计 y-idx个
8081
int rightLength = y - idx;
8182
if (rightLength > 0) {
8283
// i + leftLength, j - 1,前序遍历的右子树的起始,结束位置

0 commit comments

Comments
 (0)