Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Path Sum III problem solution

YASH PAL, 31 July 2024

In this Leetcode Path Sum III problem solution we have Given the root of a binary tree and an integer targetSum, return the number of paths where the sum of the values along the path equals targetSum. The path does not need to start or end at the root or a leaf, but it must go downwards (i.e., traveling only from parent nodes to child nodes).

Leetcode Path Sum III problem solution

Problem solution in Python.

class Solution:
    def pathSum(self, root: TreeNode, sum: int) -> int:
        if not root:
            return 0
        result = 0
        queue = deque()
        queue.append([root, []])
        while queue:
            node, sum_list = queue.popleft()
            new_sum_list = sum_list[:]
            new_sum_list.append(0)
            for k in range(len(new_sum_list)):
                new_sum_list[k] += node.val
                if new_sum_list[k] == sum:
                    result += 1
            if node.left:
                queue.append([node.left, new_sum_list])
            if node.right:
                queue.append([node.right, new_sum_list])
        return result

Problem solution in Java.

int count = 0;
public int pathSum(TreeNode root, int sum) {
    sumPath(root, sum, new ArrayList<Integer>());
    return count;
}

public void sumPath(TreeNode root, int sum, List<Integer> path) {
    if(root == null) return;
    
    path.add(root.val);
    int curSum = 0;
    for(int i = path.size()-1; i >= 0; i--) {
        curSum += path.get(i);
        if(curSum == sum) count++;
    }
    sumPath(root.left, sum, path);
    sumPath(root.right, sum, path);
    path.remove(path.size() -1);
}

Problem solution in C++.

int DFS(TreeNode* root, int sum,int count) {
        if(root==NULL) return 0;
        if(sum-root->val==0) count++;
        return count+DFS(root->left,sum-root->val,0)+DFS(root->right,sum-root->val,0);
    }
    
    int pathSum(TreeNode* root, int sum) {
        if(root==NULL) return 0;
        return DFS(root,sum,0)+pathSum(root->left,sum)+pathSum(root->right,sum);
    }

Problem solution in C.

int pathSumHelper(struct TreeNode* root, int sum, int cur, int inc){
    int ret = 0;
    
    if (root == NULL)
        return 0;

    if ((cur + root->val) == sum)
        ret = 1;

    ret += pathSumHelper(root->left, sum, cur + root->val, inc + 1);
    ret += pathSumHelper(root->right, sum, cur + root->val, inc + 1);
    
    if (inc == 0) {
        ret += pathSumHelper(root->left, sum, 0, 0);
        ret += pathSumHelper(root->right, sum, 0, 0);
    }
    
    return ret;
}

int pathSum(struct TreeNode* root, int sum){
    return pathSumHelper(root, sum, 0, 0);
}

coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes