💯JAVA/Python/JavaScript Solution Explained in HINDI
For explanation, please watch the above video and do like, share and subscribe the channel. ❤️ Also, please do upvote the solution if you liked it.Subscribe:-
https://youtu.be/1sjhFjazOvo
For explanation, please watch the above video and do like, share and subscribe the channel. ❤️ Also, please do upvote the solution if you liked it.

# Subscribe:- [ReelCoding](https://www.youtube.com/@reelcoding?sub_confirmation=1)

## Subscribe Goal:- 700
## Current Subscriber:- 603

# Complexity
- Time complexity: O(N), where N is the number of nodes in the tree
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(H), where H is the height of the tree.
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
class Solution {
    public boolean isBalanced(TreeNode root) {
        return height(root) != -1;
    }

    private int height(TreeNode node) {
        if(node == null) return 0;

        int leftHeight = height(node.left);
        if(leftHeight == -1) return -1;

        int rightHeight = height(node.right);
        if(rightHeight == -1) return -1;

        if(Math.abs(leftHeight - rightHeight) > 1) {
            return -1;
        }

        return Math.max(leftHeight, rightHeight) + 1;
    }
}
```
```Python []
class Solution:
    def isBalanced(self, root: Optional[TreeNode]) -> bool:
        def height(node):
            if not node:
                return 0

            leftHeight = height(node.left)
            if leftHeight == -1:
                return -1

            rightHeight = height(node.right)
            if rightHeight == -1:
                return -1

            if abs(leftHeight - rightHeight) > 1:
                return -1

            return max(leftHeight, rightHeight) + 1

        return height(root) != -1       
```

```JavaScript []
var isBalanced = function(root) {
    function height(node) {
        if(node === null) return 0;

        let leftHeight = height(node.left);
        if(leftHeight === -1) return -1;

        let rightHeight = height(node.right);
        if(rightHeight === -1) return -1;

        if(Math.abs(leftHeight - rightHeight) > 1) return -1;

        return Math.max(leftHeight, rightHeight) + 1;
    }
    return height(root) !== -1;
};
```

['Java']