Balanced Binary Tree

Easy

Topics
TreeDFS

Given a binary tree, determine if it is height-balanced — for every node, the heights of its two subtrees differ by at most 1.

Example 1

9315207
Every node’s two subtree heights differ by at most one, so the answer is true. Computing heights top-down re-walks the tree repeatedly; returning height from a post-order pass and failing fast keeps it linear.
Tree given in level-order (breadth-first); null marks a missing child.
Input:  root = [3,9,20,null,null,15,7]
Output: true

Example 2

4342312
subtree heights differ by more than 1
At the shaded node the two subtree heights differ by more than one, so the answer is false. The condition has to hold at every node, not just the root.
Input:  root = [1,2,2,3,3,null,null,4,4]
Output: false

Constraints

  • 0 <= number of nodes <= 5000
  • -10^4 <= Node.val <= 10^4
Editorial — intuition, approach, complexity and pitfalls

Intuition

A naive check recomputes height at every node (O(n²)). Instead, compute height bottom-up and return a sentinel (-1) the moment any subtree is unbalanced, so the whole tree short-circuits.

Approach

  1. Write a helper returning the height of a subtree, or -1 if it (or anything below) is unbalanced.
  2. For a node, get left and right heights; if either is -1, or they differ by more than 1, return -1.
  3. Otherwise return 1 + max(left, right). The tree is balanced iff the root helper is not -1.

Dry Run

A single return value carries two messages: a height, or -1 meaning "something
below me is already unbalanced". That overloading is what buys the O(n).
tree 3 9 20 null null 15 7  --  9 is a leaf, 20 has children 15 and 7
      node 9    lh=0 rh=0  ok  -> return 1
        node 15   lh=0 rh=0  ok  -> return 1
        node 7    lh=0 rh=0  ok  -> return 1
      node 20   lh=1 rh=1  ok  -> return 2
    node 3    lh=1 rh=2  differ by 1, ok  -> return 3
  helper returned 3, not -1  ->  balanced
tree 1 2 2 3 3 null null 4 4
          node 4    lh=0 rh=0  ok  -> return 1
          node 4    lh=0 rh=0  ok  -> return 1
        node 3    lh=1 rh=1  ok  -> return 2
        node 3    lh=0 rh=0  ok  -> return 1
      node 2    lh=2 rh=1  ok  -> return 3
      node 2    lh=0 rh=0  ok  -> return 1
    node 1    lh=3 rh=1  differ by 2  -> return -1
  helper returned -1  ->  not balanced
Notice that no node is measured twice. The naive version asks "what is my left
height, what is my right height" and computes each by walking the subtree again,
which is O(n) work per node and O(n^2) overall. Here the height arrives from
below as the return value of a visit that was going to happen anyway.
The two `if lh == -1` / `if rh == -1` checks are the short-circuit. Once any
subtree reports -1 the sentinel propagates straight to the root without any
further comparison, so a tree that is unbalanced deep on the left never has its
right side examined at all.

Complexity

  • Time — O(n): a single bottom-up pass; heights are computed once.
  • Space — O(h): recursion stack.

Common Pitfalls

  • The naive top-down version recomputes heights and is O(n²) — use the -1 sentinel to stay O(n).

Full Solution — all languages

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:
    def isBalanced(self, root):
        def h(node):
            if not node:
                return 0
            lh = h(node.left)
            if lh == -1:
                return -1
            rh = h(node.right)
            if rh == -1:
                return -1
            if abs(lh - rh) > 1:
                return -1
            return 1 + max(lh, rh)
        return h(root) != -1
SensAI
Beta
Tap the mic to stop voice mode