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

Example: [3,9,20,null,null,15,7] โœ… Balanced
Example: [1,2,2,3,3,null,null,4,4] โŒ Not balanced

This 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
RootLRh=1h=1h=1h=1โœ“ |1-1|=0โ‰ค1โœ“ |1-1|=0โ‰ค1โœ“ |2-2|=0โ‰ค1๐ŸŽฏ Bottom-Up Check:1. Calculate leaf heights (always 1)2. Check parent balance: |left_h - right_h| โ‰ค 1Balance FormulaFor each node:|left_height - right_height| โ‰ค 1AND both subtrees balancedโœ… Tree is BALANCED
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

n
2n
โœ“ Linear Growth
Space Complexity
O(h)

Recursion stack depth equals tree height h

n
2n
โœ“ 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
Asked in
Amazon 45 Microsoft 38 Google 32 Meta 28
67.4K Views
High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen