Binary Tree · Traversal

Balanced Binary Tree

Check whether every node has left and right subtree heights differing by at most one.

DFSBottom-up RecursionInterview Essential

Key concepts at a glance — for those who already know the basics.

Problem Statement

Balanced Binary Tree

Given the root of a binary tree, return true if it is height-balanced, otherwise return false.

Balance Rule: for every node, |height(left) - height(right)| <= 1

01

Intuition

Bottom-up DFS

Use post-order recursion so each node receives left and right heights from children.

Sentinel -1

Return -1 when unbalanced. Parents immediately propagate -1 without extra work.

One Pass

Compute heights and validate balance together in O(n).

02

Diagram

Balanced - true

3920157|1-2|=1 <= 1

Not balanced - false

1223344|3-1|=2 > 1
03

Dry Run

CallLeftRight|diff|Return
check(9)0001
check(15)0001
check(7)0001
check(20)1102
check(3)1213 (balanced)
04

Complexity

Time Complexity

O(n)

Every node is visited once in the recursive pass.

Space Complexity

O(h)

Recursion stack height. O(log n) balanced, O(n) skewed.

Interview tip: Mention naive O(n^2) top-down first, then immediately improve to one-pass bottom-up DFS with sentinel -1.

Ready to see it in action?

Step through the visualizer to watch the algorithm state update live.

Open Visualizer