> For the complete documentation index, see [llms.txt](https://soumyajit4419.gitbook.io/ds-algo/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://soumyajit4419.gitbook.io/ds-algo/binary-tree/problems-related-to-binary-trees/8.balanced-binary-tree.md).

# 8.Balanced Binary Tree

A binary tree in which the **left and right subtrees** of **every node** **differ in height** by not **more than 1**.

```
Example 1:
Given the following tree [3,9,20,null,null,15,7]:

    3
   / \
  9  20
    /  \
   15   7
Return true


Example 2:
Given the following tree [1,2,2,3,3,null,null,4,4]:

       1
      / \
     2   2
    / \
   3   3
  / \
 4   4
Return false
```

### Solution :

```cpp
class Solution
{

public:
    
    int findHeight(TreeNode *root){
        
        if(root == NULL){
            return 0;
        }
        
        int lh = findHeight(root->left);
        int rh = findHeight(root->right);
        
        return max(lh,rh) + 1;
    }
    
    bool isBalanced(TreeNode *root)
    {

        if (root == NULL)
        {
            return true;
        }

        int lh = findHeight(root->left);
        int rh = findHeight(root->right);

        int x = lh - rh;

        if (abs(x) <= 1 && (isBalanced(root->left)) && (isBalanced(root->right)))
        {
            return true;
        }

    
        return false;
    }
};
```

**Time Complexity: O(n^2)**

## Solution: (Finding height  and checking balanced)

```cpp
class Solution
{
public:
    bool res = true;
    int findHeight(TreeNode *root)
    {

        if (root == NULL)
        {
            return 0;
        }

        int lh = findHeight(root->left);
        int rh = findHeight(root->right);

        if (abs(lh - rh) > 1)
        {
            res = false;
        }

        return max(lh, rh) + 1;
    }

    bool isBalanced(TreeNode *root)
    {

        if (root == NULL)
        {
            return true;
        }

        findHeight(root);

        return res;
    }
};
```

**Time Complexity: O(n)**


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://soumyajit4419.gitbook.io/ds-algo/binary-tree/problems-related-to-binary-trees/8.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.
