DSA Tracker

Problem

Easy

Balanced Binary Tree

An easy Binary Trees interview guide. Task statement, worked examples, intuition, and step-by-step solutions.

Topic
Binary Trees
Sheets
3
Core for
4 roles

The problem

Given the root of a binary tree, determine if it is height-balanced. A height-balanced binary tree is defined as a binary tree in which the left and right subtrees of every node differ in height by no more than 1.

Example 1

Input
[5,3,8,2,4,7,9,1]
Output
true
Why
No node's two sides differ in height by more than one; the deepest leaf 1 is only one level below the rest.

Example 2

Input
[1,2,null,3,null,4]
Output
false
Why
Left chain 1->2->3->4 has depth 3, right is empty depth 0. Diff = 3 > 1.

Example 3

Input
[]
Output
true
Why
An empty tree is trivially balanced.

Constraints

  • The number of nodes is in the range [0, 5000].
  • -104 <= Node.val <= 104

How to think about it

Updated 2026-09-29

Checking balance top-down forces repeated depth scans on every subtree. Instead, piggyback the balance check onto height calculation bottom-up: if any subtree is unbalanced, propagate a sentinel value (-1) straight to the root to short-circuit all remaining work.

Approaches, worst first

  1. Top-down recursive depth check

    time O(n^2) · space O(h)

    At each node, call `depth(node.left)` and `depth(node.right)`, check that `abs(left - right) <= 1`, and recurse on children. Recomputes depths of lower nodes repeatedly, degrading performance on skewed trees.

  2. Bottom-up DFS with sentinelWrite this one

    time O(n) · space O(h)

    Compute height recursively. If a child returns -1 or subtree heights differ by more than 1, return -1 immediately. Otherwise return `1 + max(left, right)`. The tree is balanced if and only if the root call does not evaluate to -1.

Where people lose marks · 3
  • A tree where every individual node's left and right depths differ by at most 1 is balanced, but only checking height at the root fails when deeper subtrees are lopsided.
  • Confusing 0 depth with the -1 failure sentinel; an empty subtree has height 0, which is perfectly valid and balanced.
  • Failing to propagate -1 immediately upon detecting an imbalance wastes time computing heights for sibling subtrees.

The theory behind it

Binary Trees — the ground this problem stands on. All Binary Trees problems

What Binary Trees is

A binary tree is a branching data structure that starts at a single top node called the root, like an upside-down family tree. Every node holds a piece of data and can branch out to at most two children below it, known as the left child and the right child. Because there is no ordering rule about which values go left or right, finding a specific item can require checking every single node in the entire tree.

When to reach for it

Reach for binary trees when problems present hierarchical data with left and right child pointers. Questions asking for tree height, maximum depth, path sums from root to leaf, diameter, lowest common ancestor, or checking whether two trees are mirror reflections of each other all signal binary tree traversals. Any problem asking to inspect or reconstruct a tree layer by layer or path by path belongs here.

How the pattern works

Think recursively by focusing on what a single node must do. If the current node is null, return the base answer immediately. Otherwise, ask the left child for its result, ask the right child for its result, and combine both answers with the current node value before returning up to the parent. For horizontal scans, use a queue to read nodes layer by layer, measuring the queue length at the start of each layer to group nodes by depth.

More Binary Trees problems

Track in your role's order

Pick your target role and all 370 problems resequence to what that interview actually asks.

Start free

Problem set and role mapping as of .