-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0437-path-sum-iii.py
37 lines (30 loc) · 1.62 KB
/
0437-path-sum-iii.py
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
37
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> int:
def dfs(node, cur_sum, prefix_sum_counter):
nonlocal count
if not node:
return
# Update the current path sum
cur_sum += node.val
# Check if there exists a subarray summing to targetSum
count += prefix_sum_counter.get(cur_sum - targetSum, 0)
# Update the prefix sum counter
prefix_sum_counter[cur_sum] = prefix_sum_counter.get(cur_sum, 0) + 1
# Recur for the left and right children
dfs(node.left, cur_sum, prefix_sum_counter)
dfs(node.right, cur_sum, prefix_sum_counter)
# Backtrack: remove the current node's value from the prefix sum counter
prefix_sum_counter[cur_sum] -= 1
if prefix_sum_counter[cur_sum] == 0:
del prefix_sum_counter[cur_sum]
count = 0
# Initialize with a zero sum having a frequency of 1
prefix_sum_counter = {0: 1}
dfs(root, 0, prefix_sum_counter)
return count