3. What will be the output of the following code snippet?
struct Node {
int data;
struct Node* left;
struct Node* right;
};
int height(struct Node* root) {
if (!root) return 0;
int leftHeight = height(root->left);
int rightHeight = height(root->right);
return (leftHeight > rightHeight ? 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);
}
// Tree:
// 1
// / \
// 2 3
// /
// 4
int main() {
struct Node n4 = {4, NULL, NULL};
struct Node n2 = {2, &n4, NULL};
struct Node n3 = {3, NULL, NULL};
struct Node n1 = {1, &n2, &n3};
printf("%d", isBalanced(&n1));
return 0;
}