π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 3k = 3
Output:
2Explanation:
Path 1: 1 β 2 (Sum = 3)
Path 2: 3 (Sum = 3)
Example 2:
Input: k = 7
Output:
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++)
π² Alternative Approaches
2οΈβ£ Brute Force Recursive Approach
Note: This approach recalculates many subpaths and is too slow for large trees.
β Optimized than brute force but slower than the prefix sum approach
β οΈ Still has overlapping subproblems (may hit TLE in large trees).
Note: Not Passing All Test Cases
Test Cases Passed:1110 /1120Time limit exceeded.
3οΈβ£ Iterative DFS with Path Vector
Note: This method tracks the full path from the root, which leads to high memory usage and slower runtime.
π₯ Iterative implementation avoids recursion stack overflow.
β οΈ Higher space complexity due to storing entire paths.
π Comparison of Approaches
1οΈβ£ Prefix Sum (Optimized)
π’ O(N)
π‘ O(H + N)
DFS + HashMap
Fastest, avoids redundant work
Slightly complex to implement
2οΈβ£ Count from Each Node (Recursive)
π‘ O(NΒ²)
π‘ O(H)
Pure Recursion
Simple, intuitive
Overlapping subproblems, slower
3οΈβ£ Iterative DFS (Path Tracking)
π‘ O(NΒ²)
π΄ O(NΒ·H)
Stack-based
No recursion depth issues
High memory usage for large paths
π‘ Best Choice?
Balanced Trees / Small Input: β Approach 1 (Prefix Sum) is the fastest. For optimal performance on large trees, use the Prefix Sum DFS approach.
Unbalanced / Deep Trees: β Approach 3 (Iterative DFS) avoids stack overflow.
Simple Understanding: β Approach 2 helps with conceptual clarity but may be slow.
Code (Java)
Code (Python)
π― 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