Balanced Binary Tree (LeetCode 110): Solution and Explanation

Easy
Tree
Depth First Search
Binary Tree

A binary tree is height-balanced when, at every node, the heights of the left and right subtrees differ by at most one. Decide whether a given tree meets that rule.

Variables
No variables to display
DepthFunction Call
Stack empty
0/0

Intuition

The definition reads top down: check the root's two subtree heights, then check that each subtree is itself balanced. Coding it that way means calling a height function at every node, and each call walks the whole subtree underneath. On a skewed tree that adds up to O(n²), because nodes near the bottom are measured once for every ancestor.

The improvement is to flip the direction and work bottom up. A node needs two facts from each child: is that subtree balanced, and how tall is it? If the child computes both and returns them together, the parent can decide its own balance in constant time. It is balanced only if both children are balanced and their heights differ by at most one, and its height is one more than the taller child.

The reference code returns a pair [balanced, height] from every call. An empty subtree is balanced with height 0, which is the base case. Every node is visited exactly once, and no height is ever recomputed, so the whole check takes linear time.

Note that the rule must hold at every node, not just the root. A tree can have equal-height subtrees at the root and still be unbalanced deeper down, which is why each pair carries the balanced flag upward from the children. Once any subtree reports false, every ancestor also reports false, so a single violation anywhere is enough to fail the whole tree, exactly as the definition requires.

Approach: Bottom-up DFS returning balance and height

  1. Define dfs(root) that returns [balanced, height] for the subtree.
  2. If root is None, return [True, 0].
  3. Recursively compute left = dfs(root.left) and right = dfs(root.right).
  4. Set balanced to true only when left[0] and right[0] are both true and abs(left[1] - right[1]) <= 1.
  5. Return [balanced, 1 + max(left[1], right[1])].
  6. The answer is dfs(root)[0].

Step-by-step example

root = [1,2,2,3,3,null,null,4,4]
  1. The two deepest leaves with value 4 return [True, 1] each, so their parent 3 (the left child of the left 2) returns [True, 2].
  2. The other 3, a leaf, returns [True, 1].
  3. The left 2 sees heights 2 and 1, a difference of 1, so it returns [True, 3].
  4. The right 2 is a leaf and returns [True, 1].
  5. The root sees heights 3 and 1, a difference of 2, so it returns [False, 4] and the function returns false.

Code: Python, Java, C++

1class Solution:
2    def isBalanced(self, root: Optional[TreeNode]) -> bool:
3        def dfs(root):
4            if not root:
5                return [True, 0]
6            
7            left, right = dfs(root.left), dfs(root.right)
8            balanced = (left[0] and right[0] and 
9                       abs(left[1] - right[1]) <= 1)
10            
11            return [balanced, 1 + max(left[1], right[1])]
12            
13        return dfs(root)[0]

Time and space complexity

Time

O(n)

Every node is visited once and computes its result from its children's results in constant time.

Space

O(h)

The recursion stack depth equals the tree height, which is O(n) in the worst case of a skewed tree.

Other approaches

Top-down height checks (time O(n²), space O(h))

At each node compute the heights of both subtrees with a separate function, check the difference, then recurse into both children. It matches the definition word for word but recomputes heights, giving quadratic time on skewed trees.

Height with -1 sentinel (time O(n), space O(h))

Return the height normally, but return -1 when a subtree is unbalanced and propagate it immediately. It packs both facts into one integer and lets the recursion stop early once imbalance is found.

Common mistakes

  • Only comparing the heights of the root's two subtrees. The balance rule applies at every node, so deeper imbalances must be detected too.
  • Confusing balanced with complete or full. A balanced tree can have missing nodes anywhere, as long as heights differ by at most one at each node.
  • Returning the height of an empty subtree as -1 in one place and 0 in another, which shifts differences and gives wrong answers on uneven trees.
  • Dropping the children's balance flags and only checking the height difference at the current node.

Frequently asked questions

What is a height-balanced binary tree?

It is a tree where, for every node, the heights of the left and right subtrees differ by no more than one. The condition applies at every node, not only at the root.

Why is the bottom-up approach O(n)?

Each call returns both the balance flag and the height, so no height is ever recomputed. Every node is processed once in constant time.

Can we stop early when the tree is unbalanced?

Yes. Returning a sentinel like -1 for an unbalanced subtree lets parents skip further work. The worst case is still O(n), but unbalanced trees often finish sooner.

Is an empty tree balanced?

Yes. An empty tree has no nodes that could violate the rule, so the base case returns true with height 0.