Skip to content
Decoder

Balanced Binary Tree

Easy

Problem

A binary tree is height-balanced if, at every node, the heights of the left and right subtrees differ by at most 1. Given the root, return whether the tree is height-balanced.

Trees in the examples are written in level order (top to bottom, left to right), with null for a missing child, the same way the tests build them.

Example 1

Input
[3, 9, 20, null, null, 15, 7]
Output
True

Example 2

Input
[1, 2, 2, 3, 3, null, null, 4, 4]
Output
False
Why
left side is 3 deep, right is 1

Example 3

Input
[]
Output
True

Constraints

  • 0 <= number of nodes <= 5000
  • -10^4 <= Node.val <= 10^4

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.