# Height Balanced Binary Tree

![](/files/NGbehyYKvbJ6YUJ1iPBm)

* 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;
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://algorithm.prettylog.com/algorithm-problems/algoexpert/medium/height-balanced-binary-tree.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
