Javascript solution


  • 0
    R
    var isBalanced = function(root) {
        
        if(root === null)
            return true;
        
        var right = treeSize(root.right),
            left = treeSize(root.left),
            difference = Math.abs( right - left ),
            result = true;
        
        if(difference > 1)
            result = false;
        
        return result && isBalanced(root.left) && isBalanced(root.right);
        
    };
    
    var treeSize = function(root){
        if(root === null)
            return 0;
        return 1 + Math.max(treeSize(root.right), treeSize(root.left));
    };

Log in to reply
 

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