[Explanation] Detailed Javascript Solution
\n/**\n * DFS - Preorder\n * \n * pathSum: sum of all values from current node to root\n * map(pathSum - targetSum): frequency of targetSum in the path (up to c
```\n/**\n * DFS - Preorder\n * \n * pathSum: sum of all values from current node to root\n * map(pathSum - targetSum): frequency of targetSum in the path (up to current node)\n * \n * Case 1: valid tree path sum that starts from the root node\n *          - pathSum === targetSum\n *             => increment output by 1\n * Case 2: valid tree path sum that starts middle of the tree\n *          - check for frequency of target sum in the current path\n *             * if True, it means that we found targetSum \n *                * map[pathSum - targetSum] number of times\n *             => increment output by frequency of map(pathSum - targetSum)\n * *Time: O(N)\n * *Space: O(N)\n */\n// Runtime: 134 ms, faster than 51.52% of JavaScript online submissions for Path Sum III.\n// Memory Usage: 47.5 MB, less than 12.82% of JavaScript online submissions for Path Sum III.\nconst pathSum = (root, targetSum) => {\n\tlet output = 0;\n\tlet map = {};\n\n\tconst traverse = (root, pathSum) => {\n\t\tif (!root) return null;\n\n\t\tpathSum += root.val; // current path sum\n\n\t\t// Case 1: starts from root node\n\t\tif (pathSum === targetSum) output++;\n\n\t\t// Case 2: starts from middle of tree\n\t\t// frequency of targetSum in the current path (up to current node)\n\t\tif (map[pathSum - targetSum]) output += map[pathSum - targetSum];\n\n\t\t// memoize current path sum (root to current node) and it\'s frequency\n\t\tif (map[pathSum]) map[pathSum]++;\n\t\telse map[pathSum] = 1;\n\n\t\tif (root.left) traverse(root.left, pathSum);\n\t\tif (root.right) traverse(root.right, pathSum);\n\n\t\t// remove the current path sum\n\t\t// to note that path is not available/visited\n\t\tmap[pathSum]--;\n\t};\n\n\ttraverse(root, 0);\n\treturn output;\n};\n```
['JavaScript']