Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Path Sum problem solution

YASH PAL, 31 July 202419 January 2026

In this Leetcode Path Sum problem solution, we have given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum. A leaf is a node with no children.

Leetcode Path Sum problem solution

Leetcode Path Sum problem solution in Python.

class Solution:
    def hasPathSum(self, root: TreeNode, sum: int) -> bool:
        def helper(node, target):
            if not node:
                return False
            
            target -= node.val
            
            if (target == 0) and (not node.left) and (not node.right):
                return True
            
            return helper(node.left, target) or helper(node.right, target)
        
        if not root:
            return
        
        return helper(root, sum)

Path Sum problem solution in Java.

class Solution {
    public boolean hasPathSum(TreeNode root, int sum) {
        return hasPathSum(root, sum, 0);
    }
    
    private boolean hasPathSum(TreeNode root, int sum, int temp) {
        if(root == null) {
            return false;
        }
        temp += root.val;
        if(temp == sum && root.left == null && root.right == null) {
            return true;
        }
        return hasPathSum(root.left, sum, temp) || hasPathSum(root.right, sum, temp);
    }
}

Problem solution in C++.

class Solution {
public:
    void f(TreeNode* root, int targetSum, int contri, int &ans) {
        if(root == NULL)
            return;
        
        if(root->left == NULL && root->right == NULL) {
            if(contri+root->val == targetSum) ans = 1;
            return;
        }
        
        f(root->right, targetSum, contri+root->val, ans);
        f(root->left, targetSum, contri+root->val, ans);
    }
    bool hasPathSum(TreeNode* root, int targetSum) {
        int ans = 0, contri = 0;
        
        f(root, targetSum, 0, ans);
        
        return ans;
    }
};

Problem solution in C.

typedef struct TreeNode t;

void getSum(t* root, int target, int* sum,bool* ret){
    if(!root->left && !root->right) {
        if((*sum) + root->val == target){
            *ret=1;
        }
        return;
    }
    (*sum)+=root->val;
    if(root->left) {
        getSum(root->left,target,sum,ret);
    }
    if(root->right) {
        getSum(root->right,target,sum,ret);
    }
    (*sum)-=root->val;
    
}

bool hasPathSum(t* root, int sum){
    if(!root) return 0;
    int s=0;
    bool ret=0;
    getSum(root,sum,&s,&ret);
    return ret;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes