Help me out in this problem



it is giving error in some case

hello @shivammishra20121999

u need to end ur search only on the leaf but in ur current code u r ending on some non leaf as well.

check this->

class Solution {
public:
    bool hasPathSum(TreeNode* root, int sum) {
        if(root==NULL){
            return false;
        }
    if(!root->left &&  !root->right ){
        return sum==root->val;
    }
        bool l=false,r=false;
        if(root->left)
            l=hasPathSum(root->left,sum-root->val);
        if(root->right)
            r=hasPathSum(root->right,sum-root->val);
            
            
        return l||r;
    }
};

@aman212yadav why root==null we have to return false?

with empty tree , we cannot reach to any sum