Skip to content
Onsite

Balanced Binary Tree

EasyP2Coding

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

Example 1

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

Example 2

Input
root = [1,2,2,3,3,null,null,4,4]
Output
false
Why
The left subtree of the root has height 3 and the right has height 1.

Example 3

Input
root = []
Output
true

Constraints

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

Hints

Run checks the 3 visible tests, Submit all 9.