My JavaScript solution


  • 0
    S
    /**
     * Definition for a binary tree node.
     * function TreeNode(val) {
     *     this.val = val;
     *     this.left = this.right = null;
     * }
     */
    /**
     * @param {TreeNode} root
     * @return {number[]}
     */
    var largestValues = function(root) {
        var hash = {},
            res = [];
    
        find(root, 1);
    
        Object.keys(hash).forEach(function(key) {
            res.push(hash[key]);
        })
    
        return res;
    
        function find(root, level) {
            if (!root) {
                return;
            }
            if (hash[level] === undefined) {
                hash[level] = root.val;
            }
    
            hash[level] = Math.max(root.val, hash[level]);
    
            find(root.left, level + 1);
            find(root.right, level + 1);
    
        }
    };
    
    

Log in to reply
 

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