Why my soln fails the [2147483647] case?


  • 0
    R

    Very weird. Tried locally and on repl both give correct results. OJ keep saying my output is [2]. Hard to believe and can't find out the reason.

    Any help will be greatly appreciated!

    /**
     * Definition for a binary tree node.
     * function TreeNode(val) {
     *     this.val = val;
     *     this.left = this.right = null;
     * }
     */
    /**
     * @param {TreeNode} root
     * @return {number[]}
     */
    const nodes = [];
    
    const explore = (node) => {
        if (!node) return;
        
        explore(node.left);
        nodes.push(node.val);
        explore(node.right);
    };
    
    const getMode = (nodes) => {
        if (!nodes || !nodes.length) return [];
        
        let max = 0;
        let maxKey = [];
        let currStreak = 0;
        
        const isEndOfStreak = (i) => {
            return i >= nodes.length - 1 || nodes[i] !== nodes[i+1];
        };
        
        for (let i = 0; i < nodes.length; i++) {
            currStreak += 1;
            if (isEndOfStreak(i)) {
                if (currStreak > max) {
                    max = currStreak;
                    maxKey = [nodes[i]];
                } else if (currStreak === max) {
                    maxKey.push(nodes[i]);
                }
                
                currStreak = 1;
            }
        }
        
        return maxKey;
    }
    
    var findMode = function(root) {
        explore(root);
        return getMode(nodes);
    };
    

Log in to reply
 

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