2Brute2Best
🔥0

Balanced Binary Tree

Easy·LeetCode #110 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

You are given the root of a binary tree. Decide whether it is height-balanced.

A tree is height-balanced when, at every node, the heights of its left side and right side differ by at most 1. An empty side has height 0. The rule applies to every node, not just the root.

Input

The root of a binary tree. It can be empty and has at most 5000 nodes.

Output

true if the tree is height-balanced, false otherwise.

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

At the root, the left side has height 1 (node 9) and the right side has height 2 (node 20). They differ by 1. Node 20 has two leaves, so it differs by 0. Every node passes.

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

The left side of the root has height 3 (2 → 3 → 4), but the right side is a single node with height 1. They differ by 2, so the tree is not balanced.

Example 3
Input: root = []
Output: true

An empty tree has no node that fails the rule.

Constraints
  • • The number of nodes is in the range [0, 5000].
  • • −10^4 ≤ Node.val ≤ 10^4
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“at every node”
→ The rule is local to each node, so every node must be checked, not only the root.
“heights differ by at most 1”
→ At each node, compare one number from the left child with one number from the right child.
“height-balanced (yes or no)”
→ The answer is a yes or no, but the check depends on heights, so each node returns its height.
03

Approach Complexity Ladder

Brute Force — measure both sides at every nodeO(N²)O(H)

At each node, measure both sides with separate searches, then check the children the same way

Optimal — return the height, or −1 when something is brokenO(N)O(H)

One post-order pass: each node returns its height, or −1 as soon as something is wrong

Brute Force Intuition

For every node, call a separate height() on its left child and on its right child. If the difference is more than 1, the tree is not balanced. Otherwise, do the same check on both children.

Recognition rule for this sub-pattern:

Each node returns a summary of its subtree (height, best path). The global answer is updated while combining.
Remember this · Tree DFS
Trigger
Paths, heights, diameters, validating structure, ancestors, anything "for every subtree".
Think
If my left and right children already gave me their answers, how do I build mine?
Key question
What does my recursive function return, and what do I pass down?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Tree DFS translate into production architectures and low-level memory mechanics.