Given a binary tree, determine if it is height-balanced.
Example 1:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Input: root = [3,9,20,null,null,15,7]
Output: true
Example 2:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Input: root = [1,2,2,3,3,null,null,4,4]
Output: false
Example 3:
Input: root = []
Output: true
Constraints:
- The number of nodes in the tree is in the range
[0, 5000]
.
-104 <= Node.val <= 104
根據題意,左側樹高和右側樹高的差如果大於 1 即沒有平衡,使用遞迴解。