Most Frequent Subtree Sum LeetCode solution in JavaScript
1var findFrequentTreeSum = function (root) {2const hash = new Map();3const result = [];4let max = 0;5const dfs = (node = root) => {6if (!node) return 0;7const { left, right, val } = node;8const sum = val + dfs(left) + dfs(right);9const count = hash.get(sum) ?? 0;10hash.set(sum, count + 1);11max = Math.max(max, count + 1);12return sum;13};1415dfs();16hash.forEach((value, key) => value === max && result.push(key));17return result;18};