3lines C++


  • 0
    X

    double DFS

    class Solution {
    public:
        int pathSum(TreeNode* root, int sum) {
            return !root?0:_dfs(sum, root) + pathSum(root->left, sum) + pathSum(root->right, sum);
        }
        int _dfs(int sum, TreeNode *root) {
            return !root?0:(sum == root->val) + _dfs(sum - root->val, root->left) + _dfs(sum - root->val, root->right);
        }
    };
    

Log in to reply
 

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