githubEdit

🚀Day 9. K Sum Paths 🧠

The problem can be found at the following link: Question Linkarrow-up-right

💡 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

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

  1. 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.

  2. 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.

  3. 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).

  4. 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++)

chevron-right🌲 Alternative Approacheshashtag

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 /1120

  • Time 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

Approach
Time Complexity
Space Complexity
Method
Pros
Cons

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 Questionsarrow-up-right. Let’s make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


📍Visitor Count

Last updated