Discover how a simple check can save you from hours of confusing manual work!
Why Check if Binary Tree is Balanced in DSA Typescript?
Imagine you have a family tree drawn on paper. You want to quickly find if the tree is well balanced, meaning no branch is too long compared to others. Doing this by measuring every branch manually is tiring and confusing.
Manually checking each branch length takes a lot of time and you can easily make mistakes. You might miss some branches or forget to compare lengths properly, leading to wrong conclusions.
Using a simple program to check if the tree is balanced saves time and avoids errors. It automatically measures the height of each branch and compares them, telling you if the tree is balanced or not.
function isBalancedManual(root) {
// Manually check each branch length and compare
// Very long and error-prone
}function isBalanced(root) {
// Recursively check heights and balance
// Efficient and reliable
}This lets you quickly ensure your tree structure is balanced, which helps keep operations fast and efficient.
In a phone contact list stored as a tree, checking balance helps keep search times short so you find contacts quickly.
Manual checking is slow and error-prone.
Automated balance check uses recursion to compare branch heights.
Balanced trees keep data operations fast and efficient.
Practice
Solution
Step 1: Understand balance condition
A balanced binary tree means no node has left and right subtree heights differing by more than 1.Step 2: Compare options
The height difference between left and right subtrees of every node is at most 1 correctly states this condition. Other options describe different tree properties.Final Answer:
The height difference between left and right subtrees of every node is at most 1 -> Option AQuick Check:
Balance = height difference ≤ 1 [OK]
- Confusing balanced with complete tree
- Thinking balance means equal number of nodes
- Assuming all nodes must have two children
Solution
Step 1: Identify correct parameter and return types
The function should accept a TreeNode or null and return a boolean indicating balance.Step 2: Match options
function isBalanced(root: TreeNode | null): boolean matches this signature. Others have wrong parameter types or return types.Final Answer:
function isBalanced(root: TreeNode | null): boolean -> Option BQuick Check:
Input: TreeNode|null, Output: boolean [OK]
- Using number as input instead of TreeNode
- Returning number instead of boolean
- Using void return type
class TreeNode {
val: number;
left: TreeNode | null;
right: TreeNode | null;
constructor(val: number) {
this.val = val;
this.left = null;
this.right = null;
}
}
function checkHeight(node: TreeNode | null): number {
if (!node) return 0;
const leftHeight = checkHeight(node.left);
if (leftHeight === -1) return -1;
const rightHeight = checkHeight(node.right);
if (rightHeight === -1) return -1;
if (Math.abs(leftHeight - rightHeight) > 1) return -1;
return Math.max(leftHeight, rightHeight) + 1;
}
function isBalanced(root: TreeNode | null): boolean {
return checkHeight(root) !== -1;
}
const root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.left.left = new TreeNode(5);
console.log(isBalanced(root));Solution
Step 1: Analyze tree structure
The tree is left-heavy: root.left.left.left exists but right subtree is shallow, causing imbalance.Step 2: Trace checkHeight calls
checkHeight returns -1 when height difference > 1, so isBalanced returns false.Final Answer:
false -> Option DQuick Check:
Left-heavy deep subtree -> not balanced -> false [OK]
- Assuming tree is balanced without checking height difference
- Confusing return values of checkHeight
- Ignoring the -1 sentinel for imbalance
function isBalanced(root: TreeNode | null): boolean {
if (!root) return true;
const leftHeight = getHeight(root.left);
const rightHeight = getHeight(root.right);
if (Math.abs(leftHeight - rightHeight) > 1) return false;
return isBalanced(root.left) && isBalanced(root.right);
}
function getHeight(node: TreeNode | null): number {
if (!node) return 0;
return Math.max(getHeight(node.left), getHeight(node.right));
}Solution
Step 1: Review getHeight function
getHeight returns max height of children but misses adding 1 for current node.Step 2: Understand impact on balance check
Without adding 1, height is underestimated, causing wrong balance results.Final Answer:
getHeight does not add 1 to height, causing incorrect height calculation -> Option CQuick Check:
Height must include current node (+1) [OK]
- Forgetting to add 1 in height function
- Misusing Math.abs
- Missing base case for null root
Solution
Step 1: Understand inefficiency of top-down approach
Calculating height separately for each node causes repeated work and O(n²) time.Step 2: Use bottom-up approach
Return height if balanced, else -1 for imbalance, stopping early and using one traversal.Final Answer:
Use a bottom-up approach returning height or -1 for imbalance in one traversal -> Option AQuick Check:
Bottom-up single pass = efficient balance check [OK]
- Recalculating heights multiple times causing slow code
- Using breadth-first traversal which doesn't help balance check
- Storing heights globally without efficient update
Ready to prove it?
Pass the test and this pattern moves unknown
