My solution, easy to understand


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

Log in to reply
 

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