Please help in knowing what's wrong here? Not sure what needs to be done


  • 0
    V
    class Solution {
    public:
        bool hasPathSum(TreeNode *root, int sum) {
           
         
            if(!root){return false;}
               
           if (  (!root->left)  && (!root->right)   ){
               if(root->val == sum){return true;}
           }
           
           else{
               sum = sum - root->val;
                return (hasPathSum(root->left, sum) || hasPathSum(root->right, sum) );
            }
            
            
        }
    };
    

    This gives true for the following input :

    {1} 0


  • 0
    A

    In your program and the test case {1} 0 , " if ( (!root->left) && (!root->right) ) : is true,it won't enter relative else.
    It seems that the function return true as a default value if you don't return any value.


  • 0
    V

    got it! Thanks much!


Log in to reply
 

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