Javascript O(n) solution


  • 0
    let isBalanced = function(root) {
        
        let isBalancedHelper = (n) => {
            if (!n) return 0;
            
            let left = isBalancedHelper(n.left);
            if (left === -1) return -1;
         
            let right = isBalancedHelper(n.right);
            if (right === -1 || Math.abs(left - right) > 1) return -1;
            return ( 1 + Math.max(left, right));
        };
        return (isBalancedHelper(root) > -1);
    };

Log in to reply
 

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