Share my 5 lines c++ code, easy to understand


  • 2
    J
    bool hasPathSum(TreeNode *root, int sum) {
        if(root==NULL)return false;
        sum-=root->val;
        if(sum==0&&root->left==NULL&&root->right==NULL)
            return true;
        return hasPathSum(root->left,sum)||hasPathSum(root->right,sum);
    }

Log in to reply
 

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