← DiffPush

Balanced Binary Tree

Standard Bar Binary Trees · Medium Problems O(n) · O(h)

The Load-Balance Rigging Audit

A stage-rigging contractor hangs lighting trusses from a binary tree of support arms. Insurance rules say the structure is certified only if, at every junction, the two hanging chains differ in length by at most one unit — otherwise torque builds up dangerously. The auditor needs a single pass that certifies or condemns the whole rig before the show is booked.

Input: The root of a binary tree given as a level-order array where null marks a missing child. An empty array is a valid (empty) tree.

Output: Return true if the tree is height-balanced at every node, false otherwise.

Constraints

Examples

Example 1

Input: {"tree":[3,9,20,null,null,15,7]}
Output: true
Every junction's two chains differ by at most one unit.

Example 2

Input: {"tree":[1,2,2,3,3,null,null,4,4]}
Output: false
At the root, the left chain runs 3 deep but the right chain only 1 — a gap of 2.

Solve this in your browser →

Also on LeetCode ↗