Skip to content

Balanced Binary Tree

EasyTrees
Decode it first. Clues, constraints, pattern and plan before any code.

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.

[3, 9, 20, null, null, 15, 7]          →  True
[1, 2, 2, 3, 3, null, null, 4, 4]      →  False   (left side is 3 deep, right is 1)
[]                                     →  True

Constraints

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