Introduction
LeetCode 110 – Balanced Binary Tree is one of the most important binary tree interview problems.
This problem teaches:
- Tree recursion
- Height calculation
- Depth First Search (DFS)
- Bottom-up recursion
- Optimization techniques
It is frequently asked in coding interviews because it checks whether you can:
- Traverse trees efficiently
- Avoid repeated calculations
- Combine recursion with conditions
- Optimize brute force tree solutions
This problem is also a foundation for advanced tree problems like:
- AVL Trees
- Height-balanced trees
- Diameter of Binary Tree
- Tree DP problems
Problem Link
🔗 https://leetcode.com/problems/balanced-binary-tree/
Problem Statement
Given the root of a binary tree:
Return:
if the tree is:
Otherwise return:
What is a Balanced Binary Tree?
A binary tree is balanced if:
Meaning:
- Left and right subtree heights should not differ by more than:
Example 1
Input
Tree:
Output
Explanation:
Every node satisfies:
Example 2
Input
Tree:
Output
Explanation:
Left subtree becomes much deeper than right subtree.
Difference becomes greater than:
Key Observation
To determine if tree is balanced:
At every node we need:
- Height of left subtree
- Height of right subtree
- Compare difference
This naturally becomes a recursive DFS problem.
Brute Force Approach
Intuition
For every node:
- Calculate left height
- Calculate right height
- Compare difference
- Recursively check children
Brute Force Java Solution
Problem with Brute Force
The height function gets called repeatedly.
For every node:
- Heights are recalculated again and again.
This increases complexity significantly.
Brute Force Complexity
Time Complexity
because height calculation repeats.
Space Complexity
for recursion stack.
Optimized DFS Approach
Instead of:
- Calculating heights separately
We can:
- Calculate height and balance together.
Core Optimization Idea
While calculating height:
If subtree becomes unbalanced:
This avoids unnecessary computation.
Optimized Java Solution
Cleaner Optimized Version
We can simplify negative returns using:
consistently.
Cleaner Java Solution
Why This Works
At every node:
- Recursively calculate left height
- Recursively calculate right height
- If difference > 1:
- Propagate failure upward immediately.
Dry Run
Input
Step 1
Start from leaf nodes:
Step 2
Node:
gets:
Difference:
Balanced.
Height:
Step 3
At node:
Left height:
Right height:
Difference:
Unbalanced.
Return:
Final Result
Tree is:
Return:
Time Complexity Analysis
Optimized DFS Solution
Time Complexity
because every node is visited once.
Space Complexity
where:
Worst case:
for skewed tree.
Brute Force vs Optimized
| Approach | Time Complexity | Space Complexity |
| Brute Force | O(N²) | O(H) |
| Optimized DFS | O(N) | O(H) |
Interview Explanation
In interviews, explain:
Instead of recalculating heights repeatedly, we combine height calculation and balance checking into a single DFS traversal.
This demonstrates:
- Optimization skills
- Recursive DFS understanding
- Bottom-up tree processing
Common Mistakes
1. Recalculating Heights Repeatedly
This causes:
complexity.
2. Forgetting Absolute Difference
Always use:
3. Not Handling Null Nodes
Base case:
is necessary.
FAQs
Q1. What is a balanced binary tree?
A tree where left and right subtree heights differ by at most:
for every node.
Q2. Why use DFS?
Because height calculation naturally follows recursive depth traversal.
Q3. Why return -1?
It acts as a signal:
Q4. Is this problem important for interviews?
Very important.
It is one of the most common tree optimization questions.
Conclusion
LeetCode 110 is an excellent binary tree optimization problem.
It teaches:
- DFS traversal
- Height calculation
- Bottom-up recursion
- Optimization techniques
The key insight is:
Combine balance checking and height calculation in one DFS traversal.
Once you understand this optimization pattern, many advanced tree problems become much easier.





