# Height Balanced Binary Tree

![](https://3743232000-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHW2IQuh2PFpWJDvBz2FF%2Fuploads%2Fgit-blob-8485a2a5d57177761449725488ae48018b3c3a6a%2FScreenshot%202023-01-22%20at%2021.40.33.png?alt=media)

* n, h

```jsx
// 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;
```
