Fastest Recursive Approach -Beats 100% users with Java
IntuitionThe intuition behind this approach is to recursively traverse the binary tree, checking the height of each subtree along the way. At each node, we calc
![image.png](https://assets.leetcode.com/users/images/c4acefc3-da5a-4be7-9ef3-144e82c141e6_1740963975.5961828.png)


# Intuition
The intuition behind this approach is to recursively traverse the binary tree, checking the height of each subtree along the way. At each node, we calculate the height of the left and right subtrees and compare their heights. If the absolute difference between the heights is greater than 1, the tree is unbalanced. We continue this process until we reach the leaves of the tree. If at any point we find that the tree is unbalanced, we return -1. Otherwise, we return the height of the subtree. Finally, we check if the height returned for the entire tree is -1, indicating an unbalanced tree, or a valid height, indicating a balanced tree.

# Approach
<!-- Describe your approach to solving the problem. -->
The approach involves recursively checking the height of each subtree and determining if the tree is balanced or not.

# Complexity
- Time complexity:
The time complexity of the `checkHeight` function is $$O(N)$$, where `N` is the number of nodes in the tree. This function traverses each node once. Thus, the overall time complexity of the algorithm is $$O(N)$$.
  
- Space complexity:
The space complexity of the `checkHeight` function is $$O(H)$$, where `H` is the height of the tree. The space complexity is determined by the recursion stack. Thus, the overall space complexity of the algorithm is $$O(H)$$.

<div align='center'>
    <img src='https://assets.leetcode.com/users/images/132d8364-5b8a-490a-8f19-c223591355af_1702553187.1230645.jpeg' width=450/>
</div>

# Code
```java
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int checkHeight(TreeNode node){
        if(node==null) return 0;
        int leftHeight=checkHeight(node.left);
        int rightHeight=checkHeight(node.right);
        if(leftHeight==-1 || rightHeight==-1) return -1;
        if(Math.abs(leftHeight-rightHeight)>1) return -1;
        return Math.max(leftHeight,rightHeight)+1;
    }
    public boolean isBalanced(TreeNode root) {
        int height=checkHeight(root);
        if(height==-1) return false;
        else return true;
    }
}
```
['Java']