javascript O(n) solution time + O(n) space
this is a translation of https://leetcode.com/problems/path-sum-iii/discuss/141424/Python-step-by-step-walk-through.-Easy-to-understand.-Two-solutions-compariso
this is a translation of https://leetcode.com/problems/path-sum-iii/discuss/141424/Python-step-by-step-walk-through.-Easy-to-understand.-Two-solutions-comparison.-%3A-), @wonderlives explains it very well there\n```javascript\nvar pathSum = function (root, sum) {\n    const freq = { 0: 1 }\n    function dfs (root, currSum) {\n        if (!root) return 0\n        currSum += root.val\n        const oldSum = currSum - sum\n        let res = freq[oldSum] || 0\n        freq[currSum] = (freq[currSum] || 0) + 1\n        res += dfs(root.left, currSum) + dfs(root.right, currSum)\n        freq[currSum]--\n        return res\n    }\n    return dfs(root, 0)\n}\n```
['JavaScript']