Node 2's left subtree height is 1 (node 4), right subtree is 0, difference 1 ≤ 1. Node 1's left subtree height is 2, right subtree height is 1, difference 1 ≤ 1.
Step 2: Check balance recursively
All nodes satisfy balance condition, so isBalanced returns 1 (true).
Final Answer:
1 -> Option C
Quick Check:
Height difference ≤ 1 at all nodes means balanced [OK]
Hint: Check height difference ≤ 1 at every node recursively [OK]
Common Mistakes:
Ignoring recursive balance check on subtrees
Miscomputing height values
Assuming difference > 0 means unbalanced
4. Identify the bug in this code snippet that checks if a binary tree is balanced:
int height(struct Node* root) {
if (!root) return 0;
int leftHeight = height(root->left);
int rightHeight = height(root->right);
return leftHeight + rightHeight + 1;
}
int isBalanced(struct Node* root) {
if (!root) return 1;
int leftHeight = height(root->left);
int rightHeight = height(root->right);
if (abs(leftHeight - rightHeight) > 1) return 0;
return isBalanced(root->left) && isBalanced(root->right);
}
medium
A. abs function is not included from math.h
B. height function incorrectly sums left and right heights
C. isBalanced does not check for NULL root
D. isBalanced returns int instead of bool
Solution
Step 1: Analyze height function logic
The height function incorrectly returns leftHeight + rightHeight + 1 instead of max(leftHeight, rightHeight) + 1.
Step 2: Understand impact on balance check
Incorrect height causes wrong height difference, leading to wrong balance result.
Final Answer:
height function incorrectly sums left and right heights -> Option B
Quick Check:
Height = max(left, right) + 1, not sum [OK]
Hint: Height = max(left, right) + 1, not sum [OK]
Common Mistakes:
Summing heights instead of taking max
Ignoring NULL checks (though present here)
Confusing return types for boolean
5. Given a binary tree where some nodes have only one child, which approach correctly checks if it is balanced?
Choose the best method to implement isBalanced efficiently.
hard
A. Use a helper function that returns height and balance status simultaneously in one traversal
B. Calculate height for each node repeatedly in isBalanced causing O(n²) time
C. Check balance only at root node ignoring subtrees
D. Count total nodes and compare left and right subtree counts
Solution
Step 1: Understand naive approach
Calculating height separately for each node causes repeated work and O(n²) time complexity.
Step 2: Efficient approach
Using a helper function that returns both height and balance status in one traversal reduces time to O(n).
Final Answer:
Use a helper function that returns height and balance status simultaneously in one traversal -> Option A
Quick Check:
Single traversal with combined info is efficient [OK]
Hint: Combine height and balance check in one traversal [OK]
Common Mistakes:
Recomputing heights multiple times
Ignoring subtree balance checks
Using node counts instead of heights
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.