LeetCode #112 Path Sum Solution & Explanation

LeetCode Problem

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.

Constraints:

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

Solution

We can use a simple recursive approach and leverage DFS to traverse a binary tree.

C# Solution

Solution1 – recursion

public class Solution {
    public bool HasPathSum(TreeNode root, int targetSum) {
        if (root == null) {
            return false;
        }

        //minus value of current node
        targetSum -= root.val;
        
        //check if now a leaf node
        if (root.left == null && root.right == null) {
            return targetSum == 0;
        }

        return HasPathSum(root.left, targetSum) || HasPathSum(root.right, targetSum);
    }
}

According to example 3, In the first step, we check if the input is null or false.

In the next step, we minus the value of current node then we check if it equals to 0 and if it a leaf node.

Java Solution

Solution1

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

        //minus value of current node
        targetSum -= root.val;
        
        //check if now a leaf node
        if (root.left == null && root.right == null) {
            return targetSum == 0;
        }

        return hasPathSum(root.left, targetSum) || hasPathSum(root.right, targetSum);
    }
}

Python3 Solution

Solution1

class Solution:
    def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
        if(root==None):
            return False
        
        #minus value of current node
        targetSum = targetSum-root.val
        
        #check if now a leaf node
        if (root.left == None and root.right == None):
            return targetSum == 0
        
        return self.hasPathSum(root.left, targetSum) or self.hasPathSum(root.right, targetSum)

JavaScript Solution

Solution1

var hasPathSum = function(root, targetSum) {
    if (root == null) {
        return false;
    }

    //minus value of current node
    targetSum -= root.val;
    
    //check if now a leaf node
    if (root.left == null && root.right == null) {
        return targetSum == 0;
    }

    return hasPathSum(root.left, targetSum) || hasPathSum(root.right, targetSum);
};

Conclusion

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts or clicking ads, thanks you~~

If you got any problem about the explanation or you need other programming language solution, please feel free to let me know (either leave a comment or contact me by Messenger is ok!)

The problem link : Path Sum – LeetCode

Random LeetCode post

Leave a Reply

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