Height Balanced Binary Tree

  • n, h

// This is an input class. Do not edit.
class BinaryTree {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

function heightBalancedBinaryTree(node) {
  const [flag, height] = checkBalance(node);
  
  return flag;
}

function checkBalance(node) {
  if (node === null) {
    return [true, 0];
  }

  const left = checkBalance(node.left);
  const right = checkBalance(node.right);

  const diff = Math.abs(left[1] - right[1]);
  
  return [left[0] && right[0] && diff <= 1, Math.max(left[1], right[1]) + 1];
}

// Do not edit the lines below.
exports.BinaryTree = BinaryTree;
exports.heightBalancedBinaryTree = heightBalancedBinaryTree;

Last updated