Balanced Binary Tree
Problem statement
Given the root of a binary tree, determine whether it is height-balanced.
A binary tree is height-balanced when, at every node, the heights of the left and right subtrees differ by at most one.
Function
isBalanced(root: TreeNode) → booleanExamples
Example 1
root = [3,9,20,null,null,15,7]return = trueEvery node's two subtree heights differ by at most one.
Example 2
root = [1,2,2,3,3,null,null,4,4]return = falseThe left side contains a node whose subtree heights differ by more than one.
Example 3
root = []return = trueAn empty tree is height-balanced.
Constraints
- The tree contains from
0to5000nodes. -10000 <= node.val <= 10000.