Check Binary Tree Height Balance
Problem statement
Return whether every node in the tree has left and right subtree heights differing by at most1. Height counts nodes on a longest downward path; a missing subtree has height0. The empty tree is balanced. Checking only the root is insufficient.
Function
isHeightBalanced(root: TreeNode) → booleanExamples
Example 1
root = [3,9,20,null,null,15,7]return = trueAt the root the subtree heights are1 and2, and every lower node also satisfies the difference bound.
Example 2
root = [1,2,null,3]return = falseThe root has a left subtree of height2 and an empty right subtree of height0.
Constraints
0 <= serialized tree length <= 400.-1000 <= every non-null node value <= 1000.- The input is a valid acyclic binary tree serialized level by level, with null for a missing child; [] denotes an empty tree.