Skip to content
AI360Xpert
Beta
Difficulty: EasyTrees

Balanced Binary Tree

Problem in Plain English

Given the root of a binary tree, decide whether it is height-balanced, meaning the left and right subtrees of every node differ in height by at most one.

Problem Statement

Given the root of a binary tree, determine whether the tree is height-balanced.

A tree is height-balanced when, for every node, the heights of its left and right subtrees differ by no more than 1. An empty tree counts as balanced.

Constraints

  • The number of nodes is between 0 and 5000, inclusive.
  • Each node value is between -10000 and 10000, inclusive; values do not affect balance.

Examples

Example 1

Input
root = [3,9,20,null,null,15,7]
Output
true

Leaves 9, 15 and 7 have child heights 0 and 0. Node 20 has child heights 1 and 1; root 3 has child heights 1 and 2. Every gap is at most 1, so the whole tree is balanced.

Example 2

Input
root = [1,2,2,3,3,null,null,4,4]
Output
false

The leaves 4 and 4 make their parent 3 height 2. Node 2 on the left has subtree height 3, whereas the right node 2 has height 1. Root 1 has a gap of 2 and fails the rule.

Example 3

Input
root = []
Output
true

There are no nodes that can violate the height rule. The height helper returns 0 for an empty subtree and the verdict is true.

Example 4

Input
root = [1,2,2,3,null,null,3,4,null,null,4]
Output
false

The root has equal subtree heights of 3, but each child 2 has heights 2 and 0. Those internal gaps of 2 make the tree unbalanced despite a passing root comparison.

Example 5

Input
root = [1,2]
Output
true

Root 1 has heights 1 and 0, so a difference of exactly 1 is allowed. The leaf 2 is also balanced.

Intuition

Height counts nodes on the longest downward path, with an empty subtree having height 0. Balance is a local rule that must hold everywhere: in Example 1, node 20 combines heights 1 and 1, then root 3 compares heights 1 and 2. Equal heights at the root alone cannot certify its descendants. A subtree can therefore return either its height or a signal that a lower node has already failed.

Approaches

Top Down Height Check

Solution Details

Reveal Top Down Height Check: intuition, complexity, and code

Hints

Hint 1
Balance is a property of every node, not just the root, so any single-node height check must be repeated or shared.
Hint 2
A height function already visits each subtree, so let it also report imbalance instead of walking the tree twice.
Hint 3
Return -1 for an imbalanced subtree and propagate it upward at once to skip the remaining work.

Edge Cases

  • An empty tree returns true without any height comparison.
  • A single node and a two-node chain are balanced; a three-node chain is not.
  • Equal heights at the root can hide unbalanced descendants, as in Example 4.
  • Negative values and duplicate values have no effect: only the shape matters.
  • A deep one-child chain requires a deep recursive descent even for the early-exit approach; the input tree must remain unchanged.

Common Mistakes and Interview Tips

  • Checking only the root gap and forgetting that every descendant must be balanced too.
  • Mixing height conventions: these snippets use empty height 0 and leaf height 1. Use the same convention on both sides.
  • Using a gap of at least 1 as failure: exactly 1 is allowed.
  • Treating -1 as an ordinary height or overwriting the sentinel before returning it.
  • Claiming the direct code is linear: it intentionally evaluates both child checks and repeats height walks; the sentinel version removes that repetition.
  • Assuming recursion is safe at maximum depth. If the runtime stack cannot be configured, discuss an explicit post-order stack; keep any recursion-limit change outside the helper and restore it afterward.

Key Takeaway

When a tree verdict needs subtree data, return the data and the verdict together bottom-up instead of remeasuring top-down.