Challenge - 5 Problems
Balanced Tree Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ā Predict Output
intermediateOutput of balance check on a simple binary tree
What is the output of the following C code that checks if a binary tree is balanced? The function returns 1 if balanced, 0 otherwise.
DSA C
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
int max(int a, int b) {
return (a > b) ? a : b;
}
int height(Node* root) {
if (root == NULL) return 0;
return 1 + max(height(root->left), height(root->right));
}
int isBalanced(Node* root) {
if (root == NULL) return 1;
int lh = height(root->left);
int rh = height(root->right);
if (abs(lh - rh) > 1) return 0;
return isBalanced(root->left) && isBalanced(root->right);
}
int main() {
Node n1 = {1, NULL, NULL};
Node n2 = {2, &n1, NULL};
Node n3 = {3, NULL, NULL};
Node root = {4, &n2, &n3};
int result = isBalanced(&root);
printf("%d\n", result);
return 0;
}Attempts:
2 left
š” Hint
Check the height difference between left and right subtrees at each node.
ā Incorrect
The tree is balanced because the height difference between left and right subtrees of every node is at most 1.
ā Predict Output
intermediateOutput for unbalanced binary tree
What does the following C code print when checking if the binary tree is balanced?
DSA C
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
int max(int a, int b) {
return (a > b) ? a : b;
}
int height(Node* root) {
if (root == NULL) return 0;
return 1 + max(height(root->left), height(root->right));
}
int isBalanced(Node* root) {
if (root == NULL) return 1;
int lh = height(root->left);
int rh = height(root->right);
if (abs(lh - rh) > 1) return 0;
return isBalanced(root->left) && isBalanced(root->right);
}
int main() {
Node n1 = {1, NULL, NULL};
Node n2 = {2, &n1, NULL};
Node n3 = {3, NULL, NULL};
Node n4 = {4, &n2, NULL};
int result = isBalanced(&n4);
printf("%d\n", result);
return 0;
}Attempts:
2 left
š” Hint
Check the height difference at node 4.
ā Incorrect
The tree is unbalanced because the left subtree of node 4 has height 3 and right subtree is NULL (height 0), difference is 3 > 1.
š§ Conceptual
advancedTime complexity of naive balanced tree check
What is the time complexity of the naive approach to check if a binary tree is balanced, where height is computed separately for each node?
Attempts:
2 left
š” Hint
Consider that height is computed repeatedly for overlapping subtrees.
ā Incorrect
The naive approach calls height for each node, and height itself is O(n) in worst case, leading to O(n^2) overall.
š Application
advancedOptimized balanced tree check returns -1 for unbalanced
In an optimized function, height returns -1 if subtree is unbalanced. What does the function return for the following tree?
DSA C
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
int checkHeight(Node* root) {
if (root == NULL) return 0;
int lh = checkHeight(root->left);
if (lh == -1) return -1;
int rh = checkHeight(root->right);
if (rh == -1) return -1;
if (abs(lh - rh) > 1) return -1;
return 1 + (lh > rh ? lh : rh);
}
int main() {
Node n1 = {1, NULL, NULL};
Node n2 = {2, &n1, NULL};
Node n3 = {3, NULL, NULL};
Node root = {4, &n2, &n3};
int result = checkHeight(&root);
printf("%d\n", result);
return 0;
}Attempts:
2 left
š” Hint
Calculate height bottom-up and check for -1 return.
ā Incorrect
The tree is balanced, so function returns height 3.
š§ Debug
expertIdentify error in balanced tree check code
What error does the following code produce when checking if a binary tree is balanced?
DSA C
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
int height(Node* root) {
if (root == NULL) return 0;
return 1 + (height(root->left) > height(root->right) ? height(root->left) : height(root->right));
}
int isBalanced(Node* root) {
if (root == NULL) return 1;
int lh = height(root->left);
int rh = height(root->right);
if (abs(lh - rh) > 1) return 0;
return isBalanced(root->left) && isBalanced(root->right);
}
int main() {
Node n1 = {1, NULL, NULL};
Node n2 = {2, &n1, NULL};
Node n3 = {3, NULL, NULL};
Node root = {4, &n2, &n3};
int result = isBalanced(&root);
printf("%d\n", result);
return 0;
}Attempts:
2 left
š” Hint
Look carefully at the height function calls inside itself.
ā Incorrect
The height function calls height(root->left) and height(root->right) twice each, causing exponential recursion and stack overflow.
