JavaScript Solution


  • 0
    S
    var findFrequentTreeSum = function(root) {
        var res = [],
            hash = {},
            max = 0;
            
        findSum(root);
    
        for (var key in hash) {
            if (hash[key] === max) {
                res.push(parseInt(key));
            }
        }
        return res;
    
        function findSum(root) {
            var left, right;
            if (!root) return 0;
    
            left = findSum(root.left);
            right = findSum(root.right);
    
            sum = left + right + root.val;
            root.val = sum;
            hash[sum] = (hash[sum] || 0) + 1;
            max = Math.max(max, hash[sum]);
            return sum;
        }
    };
    

Log in to reply
 

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