0

I am working on Leet Code problem 112. Path Sum:

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.

I the code below trying to solve it, but apparently it isn't correct as I keep running into failed test cases.

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {

    public boolean hasPathSum(TreeNode root, int targetSum) {

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

            if(root.right!= null){
                hasPathSum(root.right, targetSum-root.val);
            }
            if(root.left!=null) {
                hasPathSum(root.left, targetSum-root.val);
            }

        } return false;
    }
}

What is wrong with this code?

trincot
  • 317,000
  • 35
  • 244
  • 286
  • if `hasPathSum(root.left, targetSum-root.val);` or `hasPathSum(root.right, targetSum-root.val);` is true you are still returning false. – c0der Nov 17 '21 at 06:19

2 Answers2

0

You are not using the values returned by the recursive calls you make. In a verbose way, you could fix that part of your code as follows:

    boolean result = false;
    if (root.right != null) {
        result = hasPathSum(root.right, targetSum - root.val);
    }
    if (!result && root.left != null) {
        result = hasPathSum(root.left, targetSum - root.val);
    }
    return result;

It is more compact when you use more logical operators for that:

    return root.right != null && hasPathSum(root.right, targetSum - root.val)
        || root.left  != null && hasPathSum(root.left,  targetSum - root.val);

It is not stated explicitly in the Leet Code challenge, but when calling this function on an empty tree, it should return false. So you should foresee the case where root is null.

The complete solution could look like this:

class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        return root != null && 
            (  root.left  == null && root.right == null && targetSum == root.val
            || root.right != null && hasPathSum(root.right, targetSum - root.val)
            || root.left  != null && hasPathSum(root.left,  targetSum - root.val)
            );
    }
}
trincot
  • 317,000
  • 35
  • 244
  • 286
0

Here is my recursive solution in javascript:

var hasPathSum = function(root, targetSum) {

    if (root) {
        if (!root.left && !root.right) {
           return root.val === targetSum;
        }

        if (root.left) {
            const isTherePath = hasPathSum(root.left, targetSum - root.val);
    
            if (isTherePath) return true;
        }

        if (root.right) {
            const isTherePath = hasPathSum(root.right, targetSum - root.val);
    
            if (isTherePath) return true;
        }
    }

    return false;
};