# Binary Tree Height-Balanced

Given a binary tree, determine if it is height-balanced.

![image](https://github.com/VikashChauhan51/algorithms/assets/14816038/7789da8e-ea56-4571-af24-7edf65c5e5e0)

### Solution

```csharp
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
public class Solution {
     public bool IsBalanced(TreeNode root)
    {
        return Balanced(root) >= 0;

    }
    public int Balanced(TreeNode root)
    {
        if (root == null)
            return 0;
        var lh = Balanced(root.left);
        if (lh == -1)
            return -1;

        var rh = Balanced(root.right);
        if (rh == -1)
            return -1;
        if ((lh > rh + 1) || (rh > lh + 1))
            return -1;
        else
            return Math.Max(lh, rh)+1;


    }
}
```


---

# 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://docs-57.gitbook.io/data-structure-and-algorithms/problems/tree/binary-tree-height-balanced.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.
