Introduction
Balanced Binary Tree means:
- height difference
between left and right subtree - should not exceed 1
Condition:
| Left Height - Right Height | <= 1Example:
1
/ \
2 3
/ \
4 5
Output:True
Explanation:
Every node has balanced subtree heights. This problem is one of the most important applications of:
DFS Traversal Constraints
1 <= Number of Nodes <= 10^5 Approach : Recursive DFS Solution
Explanations:
Explanation:
The idea is:
- recursively calculate subtree heights
- validate balance condition
Steps:
- Find left subtree height.
- Find right subtree height.
- Calculate difference.
- Check balance condition.
- Recurse entire tree.
Balanced Condition:
abs(leftHeight - rightHeight) <= 1 This approach:
- uses DFS recursion
- combines height calculation and validation
Dry Run
Visit:
1
Left height:
2
Right height:
1
Difference:
1
Balanced node.
Continue recursively for remaining nodes.
Practice :
Complexity Analysis :
Time Complexity:- O(n)Explanation : Every tree node is visited once.
Space Complexity:- O(h) Explanation : Recursion stack depends on tree height.
Why This Problem is Important