Is the Tree Height-Balanced at Every Node

Implement isHeightBalanced

You are given the root of a binary tree. Decide whether it is height-balanced: at every node, the height of the left subtree and the height of the right subtree differ by at most 1. (The height of a subtree is the number of nodes on its longest downward path; an empty subtree has height 0.) An empty tree is balanced.

Checking the two heights at every node with a separate height function repeats a lot of work. A single bottom-up pass can report a subtree's height and its balance status together.

Example 1:

Input: root = [8,4,10,2,5,9,12,1]

Output: true

Example 2:

Input: root = [6,3,null,2,null,1]

Output: false

Example 3:

Input: root = []

Output: true

+ 12 hidden test cases run on Submit.

Constraints:

  • ●0 ≤ number of nodes ≤ 100
  • ●−100 ≤ node.val ≤ 100 (the values never affect the answer)
  • ●The tree is given as its root node (null for an empty tree); each node has a val, a left child and a right child
  • ●The height of a subtree is the number of nodes on its longest downward path (0 for an empty subtree). A tree is height-balanced if at EVERY node the heights of the left and right subtrees differ by at most 1. Return true or false; an empty tree is balanced

root =

[8, 4, 10, 2, 5, 9, 12, 1]