2
public int pathSum(TreeNode root, int targetSum) {3
HashMap<Long, Integer> hm = new HashMap<>();4
// hm.put(0L,1); ---> can use this to handle initial condition if c_sum == target sum6
int res = solve(hm, root, targetSum, 0);11
public int solve(HashMap<Long, Integer> hm, TreeNode node, long tgt, long c_sum) {13
if (node == null) return 0;19
if (c_sum == tgt) // --> either this condition or the above commented condition.22
if (hm.containsKey(c_sum - tgt)) {23
res += hm.get(c_sum - tgt);26
hm.put(c_sum, hm.getOrDefault(c_sum, 0) + 1);28
int left = solve(hm, node.left, tgt, c_sum);29
int right = solve(hm, node.right, tgt, c_sum);31
res += (left + right);33
hm.put(c_sum, hm.getOrDefault(c_sum, 0) - 1); // remove the calculated cumulative sum