easy Balanced Binary Tree
Problem Statement
Determine if a binary tree is height-balanced.
A binary tree is considered height-balanced if, for each node, the difference in height between its left and right subtrees is no more than one.
Examples:
- Input:
3
/ \
9 20
/ \
15 7
- Expected Output: true
- Justification: Every node in the tree has a left and right subtree depth difference of either 0 or 1.
- Input:
1
/ \
2 2
/ \ / \
3 3 3 3
/ \ / \
4 4 4 4
- Expected Output: true
- Justification: Each node in the tree has a left and right subtree depth difference of either 0 or 1.
- Input:
1
/ \
2 5
/
3
/
4
- Expected Output: false
- Justification: The root node has a left subtree depth of 3 and right subtree depth of 1. The difference (3 - 1 = 2) exceeds 1, hence the tree is not balanced.
Constraints:
- The number of nodes in the tree is in the range
[0, 5000]. - -104 <= Node.val <= 104
Try it yourself
Try solving this question here:
✅ Solution Balanced Binary Tree
Problem Statement
Determine if a binary tree is height-balanced.
A binary tree is considered height-balanced if, for each node, the difference in height between its left and right subtrees is no more than one.
Examples:
- Input:
3
/ \
9 20
/ \
15 7
- Expected Output: true
- Justification: Every node in the tree has a left and right subtree depth difference of either 0 or 1.
- Input:
1
/ \
2 2
/ \ / \
3 3 3 3
/ \ / \
4 4 4 4
- Expected Output: true
- Justification: Each node in the tree has a left and right subtree depth difference of either 0 or 1.
- Input:
1
/ \
2 5
/
3
/
4
- Expected Output: false
- Justification: The root node has a left subtree depth of 3 and right subtree depth of 1. The difference (3 - 1 = 2) exceeds 1, hence the tree is not balanced.
Constraints:
- The number of nodes in the tree is in the range
[0, 5000]. - -104 <= Node.val <= 104
Solution
To check if a binary tree is balanced, an efficient approach involves a depth-first traversal of the tree. Starting from the root, recursively compute the height of both the left and right subtrees. If at any node, the height difference between its left and right subtree exceeds one, the tree is imbalanced. Otherwise, proceed to check its child nodes. The height of a node is the maximum height of its left or right subtree plus one. This way, we can efficiently traverse each node once and check the height difference of the left and right subtree.
- Base Case: If the node is null, return a depth of 0.
- Recursive Case: Recursively calculate the depth of the left and right subtrees. If the difference in these depths exceeds 1, or if any recursive call indicates the tree is unbalanced, mark the tree as unbalanced.
- Return the depth of the current subtree (1 plus the maximum of the depths of the left and right subtrees).
One key observation is that if any subtree is unbalanced, the entire tree is unbalanced. Thus, if at any step we discover an unbalanced subtree, we can stop our check and return false.
Algorithm Walkthrough:
For the tree:
1
/ \
2 5
/
3
/
4
-
Starting at the root node, which is
1:- We'll evaluate both the left and right subtrees.
-
For its left child (
2):- We'll examine both its left and right subtrees.
-
For the left child's left child (
3):- It has a left child,
4, with no further descendants. The maximum depth from this node is1. - It doesn't have a right child, so its depth is
0. - Difference in depths for node
3: 1 - 0 = 1 (This is within the allowable range of ≤ 1).
- It has a left child,
-
The left child (
2) of the root doesn't have a right child. Thus, its depth is0. -
For the root's left child (
2):- The maximum depth for its left subtree (from node
3to4) is2. - It doesn't have a right subtree, so its depth is
0. - Difference in depths for node
2: 2 - 0 = 2 (This difference is greater than 1, signifying that this subtree is unbalanced).
- The maximum depth for its left subtree (from node
-
For the root's right child (
5):- It doesn't have a left child or a right child, which means both depths are
0.
- It doesn't have a left child or a right child, which means both depths are
-
Comparing both children of the root node (
1):- The left subtree has a maximum depth of
3. - The right subtree has a depth of
1. - Difference in depths for node
1: 3 - 1 = 2 (This exceeds the threshold of 1).
- The left subtree has a maximum depth of
-
Considering the findings from steps 5 and 7, the algorithm determines that the tree is not balanced.
Here is the visual representation of the algorithm:
Code
Here is the code for this algorithm:
// Definition for a binary tree node.
// class TreeNode {
// int val;
// TreeNode left;
// TreeNode right;
// TreeNode(int x) { val = x; }
// }
public class Solution {
// Helper function to get the depth of the tree from a given node
public int depth(TreeNode node) {
if (node == null) return 0;
int leftDepth = depth(node.left);
// If the left subtree is unbalanced, we return -1 to indicate it's not balanced
if (leftDepth == -1) return -1;
int rightDepth = depth(node.right);
// If the rightt subtree is unbalanced, we return -1 to indicate it's not balanced
if (rightDepth == -1) return -1;
// If current node is unbalanced, return -1 to indicate it's not balanced
if (Math.abs(leftDepth - rightDepth) > 1) return -1;
// If it's balanced, we return the depth of the current subtree
return Math.max(leftDepth, rightDepth) + 1;
}
public boolean isBalanced(TreeNode root) {
return depth(root) != -1;
}
public static void main(String[] args) {
// Test example 1
TreeNode example1 = new TreeNode(3);
example1.left = new TreeNode(9);
example1.right = new TreeNode(20);
example1.right.left = new TreeNode(15);
example1.right.right = new TreeNode(7);
// Test example 2
TreeNode example2 = new TreeNode(1);
example2.left = new TreeNode(2);
example2.left.left = new TreeNode(3);
example2.left.left.left = new TreeNode(4);
example2.right = new TreeNode(5);
Solution solution = new Solution();
System.out.println(solution.isBalanced(example1)); // Expected output: true
System.out.println(solution.isBalanced(example2)); // Expected output: false
}
}
Complexity Analysis
Time Complexity: O(n), where n is the number of nodes in the tree. Each node is visited once.
Space Complexity: O(h), where h is the height of the tree. This accounts for the maximum depth of the recursive call stack.
🤖 Don't fully get this? Learn it with Claude
Stuck on Balanced Binary Tree? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Progressively stronger hints — you still solve it.
I'm working on the problem **Balanced Binary Tree** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
See the technique, not just code.
Explain the optimal approach to **Balanced Binary Tree** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Catch bugs, edge cases, sub-optimality.
I'll paste my solution to **Balanced Binary Tree**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Lock in recognition with look-alikes.
Give me 2 problems that use the SAME underlying pattern as **Balanced Binary Tree**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.