πDay 9. K Sum Paths π§
The problem can be found at the following link: Question Link
π‘ Problem Description:
Given a binary tree and an integer k, determine the number of downward-only paths where the sum of the node values in the path equals k. A path can start and end at any node within the tree but must always move downward (from parent to child).
π Example Walkthrough:
Example 1:
Input:
1
/ \
2 3
k = 3
Output:
2
Explanation:
Path 1: 1 β 2 (Sum = 3)
Path 2: 3 (Sum = 3)
Example 2:
Input: k = 7
8
/ \
4 5
/ \ \
3 2 2
/ \ \
3 -2 1
Output:
3
Explanation:
The following paths sum to k
Constraints
1 β€ number of nodes β€ $10^4$
-100 β€ node value β€ 100
-109 β€ k β€ $10^9$
π― My Approach:
Prefix Sum DFS Approach
Traverse the Tree (DFS): Use depth-first search (DFS) to traverse the binary tree. As we move from the root to each node, maintain a running sum of node values.
Maintain Prefix Sum Frequencies: Use a hash map (or dictionary) to store the frequency of each prefix sum encountered along the current path.
Key: The prefix sum value.
Value: The number of times this prefix sum has occurred.
Check for Valid Paths: For each node visited, compute the current running sum. Then check if
(current sum - k)
exists in the hash map.If it does, it indicates that there is a valid subpath ending at the current node whose sum equals k.
Increase the count by the frequency of
(current sum - k)
.
Recurse and Backtrack:
Recursively process the left and right children with the updated running sum.
After processing both subtrees, decrement the frequency of the current prefix sum in the hash map to backtrack (ensuring that paths in other branches are not affected).
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(N), as each node is visited exactly once. Expected Auxiliary Space Complexity: O(H + N), where H is the height of the tree (for recursion) and N for the hash map.
π Solution Code
Code (C++)
class Solution {
public:
unordered_map<int, int> m;
int cnt = 0;
void dfs(Node* node, int k, int currSum) {
if (!node) return;
currSum += node->data;
if (m.count(currSum - k))
cnt += m[currSum - k];
m[currSum]++;
dfs(node->left, k, currSum);
dfs(node->right, k, currSum);
m[currSum]--;
}
int sumK(Node* root, int k) {
m[0] = 1;
dfs(root, k, 0);
return cnt;
}
};
Code (Java)
class Solution {
Map<Integer,Integer> m = new HashMap<>();
int cnt = 0;
void dfs(Node r, int k, int s) {
if(r==null)return;
s += r.data;
if(m.containsKey(s-k)) cnt += m.get(s-k);
m.put(s, m.getOrDefault(s,0)+1);
dfs(r.left, k, s);
dfs(r.right, k, s);
m.put(s, m.get(s)-1);
if(m.get(s)==0)m.remove(s);
}
public int sumK(Node r, int k) {
m.put(0,1);
dfs(r, k, 0);
return cnt;
}
}
Code (Python)
class Solution:
def sumK(self, r, k):
self.c = 0
m = {0: 1}
def dfs(r, s):
if not r: return
s += r.data
self.c += m.get(s - k, 0)
m[s] = m.get(s, 0) + 1
dfs(r.left, s)
dfs(r.right, s)
m[s] -= 1
if m[s] == 0: del m[s]
dfs(r, 0)
return self.c
π― Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated