C++ 3-Lines Recursive


  • 0
    M
    class Solution {
    public:
        int pathSum(TreeNode* root, int sum, bool is_count = false) {
            if (!root) return 0;
            if (is_count) sum -= root->val;
            return (is_count ? sum == 0 : pathSum(root, sum, true)) + pathSum(root->left, sum, is_count) + pathSum(root->right, sum, is_count);
        }
    };
    

Log in to reply
 

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