304 North Cardinal St.
Dorchester Center, MA 02124

# Path Sum LeetCode Solution – Queslers

## Problem – Path Sum LeetCode Solution

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

leaf is a node with no children.

Example 1:

``````Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
Output: true
Explanation: The root-to-leaf path with the target sum is shown.
``````

Example 2:

``````Input: root = [1,2,3], targetSum = 5
Output: false
Explanation: There two root-to-leaf paths in the tree:
(1 --> 2): The sum is 3.
(1 --> 3): The sum is 4.
There is no root-to-leaf path with sum = 5.
``````

Example 3:

``````Input: root = [], targetSum = 0
Output: false
Explanation: Since the tree is empty, there are no root-to-leaf paths.
``````

Constraints:

• The number of nodes in the tree is in the range `[0, 5000]`.
• `-1000 <= Node.val <= 1000`
• `-1000 <= targetSum <= 1000`

### Path Sum LeetCode Solution in Java

``````public class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if(root == null) return false;

if(root.left == null && root.right == null && sum - root.val == 0) return true;

return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
}
}
``````

### Path Sum LeetCode Solution in Python

``````# Definition for a  binary tree node
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
# @param root, a tree node
# @param sum, an integer
# @return a boolean
# 1:27
def hasPathSum(self, root, sum):
if not root:
return False

if not root.left and not root.right and root.val == sum:
return True

sum -= root.val

return self.hasPathSum(root.left, sum) or self.hasPathSum(root.right, sum)
``````

### Path Sum LeetCode Solution in C++

``````bool hasPathSum(TreeNode *root, int sum) {
if (root == NULL) return false;
if (root->val == sum && root->left ==  NULL && root->right == NULL) return true;
return hasPathSum(root->left, sum-root->val) || hasPathSum(root->right, sum-root->val);
}
``````
##### Path Sum LeetCode Solution Review:

In our experience, we suggest you solve this Path Sum LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Path Sum LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Path Sum LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions