The test result says " {1,2}, 3 expect answer true, code output is false", but my code actually output is true.


  • 0
    L

    class Solution {
    public:
    bool hasPathSum(TreeNode *root, int sum) {
    static int res_sum = 0;
    bool res;

    if (root == NULL)
        return false;
            
    res_sum += root->val;            
    if (root->left == NULL && root->right == NULL)    
    {
        if (res_sum == sum)
            return true;
            
        else
    	{
    		res_sum -= root->val;
            return false;
    	}
    }
        
    res = hasPathSum(root->left, sum);
        
    if (res == false)
        res = hasPathSum(root->right, sum);
        
    res_sum -= root->val;
        
    return (res);
        
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.