Skip to content

Commit 67fa6a3

Browse files
committed
fd
1 parent 3000513 commit 67fa6a3

File tree

2 files changed

+45
-34
lines changed

2 files changed

+45
-34
lines changed

leetcode/solution/src/PathSumIII.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import java.util.HashMap;
2+
13
public class PathSumIII {
24

35
/**
@@ -47,35 +49,34 @@ private void helper(TreeNode root, int sum, int[] count) {
4749
helper(root.right, sum - root.val, count);
4850
}
4951

50-
/* 如果要给路径打出来
51-
public List<String> pathSum(TreeNode root, int sum) {
52-
List<String> result = new LinkedList<>();
53-
pathSum(root, sum, result, "");
54-
return result;
52+
/**
53+
* 更优化的写法,时间复杂度O(n)
54+
* 和Two Sum较类似,都是算出从root到当前节点的和,target为两段和的差值
55+
*/
56+
public int pathSum2(TreeNode root, int sum) {
57+
HashMap<Integer, Integer> map = new HashMap<>();
58+
map.put(0, 1);
59+
60+
int[] count = new int[1];
61+
helper(root, sum, 0, map, count);
62+
return count[0];
5563
}
5664

57-
private void pathSum(TreeNode root, int sum, List<String> list, String path) {
58-
if (root == null) {
65+
private void helper(TreeNode node, int target, int sum, HashMap<Integer, Integer> map, int[] count) {
66+
if (node == null) {
5967
return;
6068
}
6169

62-
pathSumWithRoot(root, sum, list, path);
63-
64-
pathSum(root.left, sum, list, "");
65-
pathSum(root.right, sum, list, "");
66-
}
67-
68-
private void pathSumWithRoot(TreeNode root, int sum, List<String> list, String path) {
69-
if (root == null) {
70-
return;
70+
sum += node.val;
71+
if (map.containsKey(sum - target)) {
72+
count[0] += map.get(sum - target);
7173
}
7274

73-
String prefix = path.isEmpty() ? "" : path + "->";
75+
map.put(sum, map.getOrDefault(sum, 0) + 1);
7476

75-
if (root.val == sum) {
76-
list.add(prefix + root.val);
77-
}
78-
pathSumWithRoot(root.left, sum - root.val, list, prefix + root.val);
79-
pathSumWithRoot(root.right, sum - root.val, list, prefix + root.val);
80-
}*/
77+
helper(node.left, target, sum, map, count);
78+
helper(node.right, target, sum, map, count);
79+
80+
map.put(sum, map.getOrDefault(sum, 0) - 1);
81+
}
8182
}

leetcode/src/Main.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,31 @@
44

55
public class Main {
66

7-
public int rob(TreeNode root) {
8-
int[] val = helper(root);
9-
return Math.max(val[0], val[1]);
7+
public int pathSum(TreeNode root, int sum) {
8+
HashMap<Integer, Integer> map = new HashMap<>();
9+
map.put(0, 1);
10+
11+
int[] count = new int[1];
12+
helper(root, sum, 0, map, count);
13+
return count[0];
1014
}
1115

12-
private int[] helper(TreeNode node) {
16+
private void helper(TreeNode node, int target, int sum, HashMap<Integer, Integer> map, int[] count) {
1317
if (node == null) {
14-
return new int[]{0,0};
18+
return;
19+
}
20+
21+
sum += node.val;
22+
if (map.containsKey(sum - target)) {
23+
count[0] += map.get(sum - target);
1524
}
16-
int[] left = helper(node.left);
17-
int[] right = helper(node.right);
18-
int[] value = new int[2];
19-
value[0] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
20-
value[1] = node.val + left[0] + right[0];
21-
return value;
25+
26+
map.put(sum, map.getOrDefault(sum, 0) + 1);
27+
28+
helper(node.left, target, sum, map, count);
29+
helper(node.right, target, sum, map, count);
30+
31+
map.put(sum, map.getOrDefault(sum, 0) - 1);
2232
}
2333

2434
public static void main(String[] args) {

0 commit comments

Comments
 (0)