Skip to content
CodeShare
All problems

Balanced Binary Tree

Check that no node's subtrees differ in height by more than one.

EasyTrees

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

Trees are written level by level, with null for a missing child.

Examples

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 root's left subtree has height 3 and its right subtree height 1.
Example 3
Input
root = []
Output
true

Constraints

  • The tree has between 0 and 5000 nodes.
  • -10^4 <= Node.val <= 10^4