Balanced Binary Tree | LeetCode 110

AFFILIATE LINKS

Great resource I use to learn algorithms.
40% off Tech Interview Pro: http://techinterviewpro.com/terriblewhiteboard
20% off CoderPro: http://coderpro.com/terriblewhiteboard

Here is the full implementation.

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
let isBalanced = function(root) {
    if (root == null) {
        return true;
    }
    
    return getHeight(root) !== -1;
};

let getHeight = function(node) {
    if (node == null) {
        return 0;
    }

    let left = getHeight(node.left);
    let right = getHeight(node.right);

    if (left === -1 || right === -1 || Math.abs(left - right) > 1) {
        return -1;
    }

    return Math.max(left, right) + 1;
};