Balanced Binary Tree - Problem
Balance Check for Binary Trees
In the world of binary trees, balance is key to maintaining optimal performance. A height-balanced binary tree is defined as a tree where the depths of the two subtrees of every node never differ by more than 1.
๐ฏ Your Mission: Given the root of a binary tree, determine if it is height-balanced.
What makes a tree balanced?
For every node in the tree:
โข The left and right subtrees' heights differ by at most 1
โข Both left and right subtrees are also balanced
This problem tests your understanding of tree traversal, recursion, and height calculation - fundamental concepts in computer science!
In the world of binary trees, balance is key to maintaining optimal performance. A height-balanced binary tree is defined as a tree where the depths of the two subtrees of every node never differ by more than 1.
๐ฏ Your Mission: Given the root of a binary tree, determine if it is height-balanced.
What makes a tree balanced?
For every node in the tree:
โข The left and right subtrees' heights differ by at most 1
โข Both left and right subtrees are also balanced
Example: [3,9,20,null,null,15,7] โ
BalancedExample: [1,2,2,3,3,null,null,4,4] โ Not balancedThis problem tests your understanding of tree traversal, recursion, and height calculation - fundamental concepts in computer science!
Input & Output
example_1.py โ Python
$
Input:
[3,9,20,null,null,15,7]
โบ
Output:
true
๐ก Note:
The tree is balanced. Root node (3) has left subtree height 1 and right subtree height 2, difference is 1 โค 1. All other nodes are also balanced.
example_2.py โ Python
$
Input:
[1,2,2,3,3,null,null,4,4]
โบ
Output:
false
๐ก Note:
The tree is not balanced. The left subtree has height 3 while the right subtree has height 0, giving a difference of 3 > 1.
example_3.py โ Python
$
Input:
[]
โบ
Output:
true
๐ก Note:
An empty tree is considered balanced by definition.
Visualization
Tap to expand
Understanding the Visualization
1
Check Leaf Balance
Start with the smallest branches (leaves) - they're always balanced with height 1
2
Move Up the Tree
For each parent branch, check if left and right children have heights differing by at most 1
3
Propagate Results
If any branch is unbalanced, the whole mobile fails - return false immediately
4
Calculate Final Height
If balanced, return the height (1 + max of children heights) for the next level up
Key Takeaway
๐ฏ Key Insight: Use bottom-up traversal to calculate height and check balance simultaneously, returning -1 for imbalanced subtrees to enable early termination and avoid redundant calculations.
Time & Space Complexity
Time Complexity
O(n)
Visit each node exactly once in the tree
โ Linear Growth
Space Complexity
O(h)
Recursion stack depth equals tree height h
โ Linear Space
Constraints
- The number of nodes in the tree is in the range [0, 5000]
- -104 โค Node.val โค 104
- Height-balanced: For every node, the depths of the two subtrees differ by at most 1
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code