c recursive solution


  • 0
    L

    ...
    bool path(struct TreeNode* root, int tmp_sum, int sum) {

    if (!root) return false;
    tmp_sum += root->val;
    if (!root->left && !root->right && (tmp_sum == sum)) return true;
    
    return path(root->left, tmp_sum, sum) || path(root->right, tmp_sum, sum);
    

    }

    bool hasPathSum(struct TreeNode* root, int sum) {

    int tmp_sum = 0;
    return path(root, tmp_sum, sum);
    

    }
    ...


Log in to reply
 

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