Problem Description
Given a binary tree, determine if it is height-balanced.
A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than one.
Examples
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
Python Solution
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
def isBalanced(root: Optional[TreeNode]) -> bool:
def checkHeight(node: Optional[TreeNode]) -> int:
if not node:
return 0
# Get heights of left and right subtrees
leftHeight = checkHeight(node.left)
if leftHeight == -1:
return -1
rightHeight = checkHeight(node.right)
if rightHeight == -1:
return -1
# Check if current node is balanced
if abs(leftHeight - rightHeight) > 1:
return -1
# Return height of current subtree
return max(leftHeight, rightHeight) + 1
return checkHeight(root) != -1
Java Solution
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
private int checkHeight(TreeNode node) {
if (node == null) {
return 0;
}
// Get heights of left and right subtrees
int leftHeight = checkHeight(node.left);
if (leftHeight == -1) {
return -1;
}
int rightHeight = checkHeight(node.right);
if (rightHeight == -1) {
return -1;
}
// Check if current node is balanced
if (Math.abs(leftHeight - rightHeight) > 1) {
return -1;
}
// Return height of current subtree
return Math.max(leftHeight, rightHeight) + 1;
}
public boolean isBalanced(TreeNode root) {
return checkHeight(root) != -1;
}
}
C++ Solution
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
private:
int checkHeight(TreeNode* node) {
if (!node) {
return 0;
}
// Get heights of left and right subtrees
int leftHeight = checkHeight(node->left);
if (leftHeight == -1) {
return -1;
}
int rightHeight = checkHeight(node->right);
if (rightHeight == -1) {
return -1;
}
// Check if current node is balanced
if (abs(leftHeight - rightHeight) > 1) {
return -1;
}
// Return height of current subtree
return max(leftHeight, rightHeight) + 1;
}
public:
bool isBalanced(TreeNode* root) {
return checkHeight(root) != -1;
}
};
JavaScript Solution
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isBalanced = function(root) {
const checkHeight = (node) => {
if (!node) {
return 0;
}
// Get heights of left and right subtrees
const leftHeight = checkHeight(node.left);
if (leftHeight === -1) {
return -1;
}
const rightHeight = checkHeight(node.right);
if (rightHeight === -1) {
return -1;
}
// Check if current node is balanced
if (Math.abs(leftHeight - rightHeight) > 1) {
return -1;
}
// Return height of current subtree
return Math.max(leftHeight, rightHeight) + 1;
};
return checkHeight(root) !== -1;
};
C# Solution
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
private int CheckHeight(TreeNode node) {
if (node == null) {
return 0;
}
// Get heights of left and right subtrees
int leftHeight = CheckHeight(node.left);
if (leftHeight == -1) {
return -1;
}
int rightHeight = CheckHeight(node.right);
if (rightHeight == -1) {
return -1;
}
// Check if current node is balanced
if (Math.Abs(leftHeight - rightHeight) > 1) {
return -1;
}
// Return height of current subtree
return Math.Max(leftHeight, rightHeight) + 1;
}
public bool IsBalanced(TreeNode root) {
return CheckHeight(root) != -1;
}
}
Complexity Analysis
- Time Complexity: O(n) where n is the number of nodes in the tree
- Space Complexity: O(h) where h is the height of the tree
Solution Explanation
This solution uses a bottom-up approach:
- Key concept:
- Check height difference
- Early termination
- Height tracking
- Algorithm steps:
- Get subtree heights
- Check balance
- Return height
- Handle base cases
Key points:
- Efficient traversal
- Height difference check
- Early termination
- Single pass solution