-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.java
More file actions
30 lines (20 loc) · 800 Bytes
/
Copy pathsolution.java
File metadata and controls
30 lines (20 loc) · 800 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
import java.util.*;
class Solution {
public int dfs(Node node, long currSum, int k, HashMap<Long,Integer> map){
if(node == null) return 0;
currSum += node.data;
int count = 0;
if(currSum == k) count++;
if(map.containsKey(currSum - k))
count += map.get(currSum - k);
map.put(currSum, map.getOrDefault(currSum,0)+1);
count += dfs(node.left, currSum, k, map);
count += dfs(node.right, currSum, k, map);
map.put(currSum, map.get(currSum)-1);
return count;
}
public int countAllPaths(Node root, int k) {
HashMap<Long,Integer> map = new HashMap<>();
return dfs(root,0,k,map);
}
}