forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
36 lines (26 loc) · 887 Bytes
/
Main.java
File metadata and controls
36 lines (26 loc) · 887 Bytes
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
import sun.plugin2.os.windows.FLASHWINFO;
import java.util.*;
public class Main {
public int pathSum(TreeNode root, int sum) {
HashMap<Integer, Integer> map = new HashMap<>();
map.put(0, 1);
int[] count = new int[1];
helper(root, sum, 0, map, count);
return count[0];
}
private void helper(TreeNode node, int target, int sum, HashMap<Integer, Integer> map, int[] count) {
if (node == null) {
return;
}
sum += node.val;
if (map.containsKey(sum - target)) {
count[0] += map.get(sum - target);
}
map.put(sum, map.getOrDefault(sum, 0) + 1);
helper(node.left, target, sum, map, count);
helper(node.right, target, sum, map, count);
map.put(sum, map.getOrDefault(sum, 0) - 1);
}
public static void main(String[] args) {
}
}