Problem Statement:
Given a binary tree, determine if it is
height-balanced.
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
Example 3:
Input: root = []
Output: true
Constraints:
- The number of nodes in the tree is in the range [0, 5000].
- -104 <= Node.val <= 104
Solution:
Algorithm:
- If the root is null, return true.
- Create a variable leftHeight to store the height of the left subtree.
- Create a variable rightHeight to store the height of the right subtree.
- Return true if the absolute difference between leftHeight and rightHeight is less than or equal to 1 and the left subtree is balanced and the right subtree is balanced, otherwise return false.
Code:
public class Solution {
public boolean isBalanced(TreeNode root) {
if (root == null) {
return true;
}
int leftHeight = height(root.left);
int rightHeight = height(root.right);
return Math.abs(leftHeight - rightHeight) <= 1 && isBalanced(root.left) && isBalanced(root.right);
}
private int height(TreeNode root) {
if (root == null) {
return 0;
}
return 1 + Math.max(height(root.left), height(root.right));
}
}
Time Complexity:
O(n^2) where n is the number of nodes in the tree.
Space Complexity:
O(n) where n is the number of nodes in the tree.
Top comments (0)