Challenge - 5 Problems
Binary Tree Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of inserting nodes in a binary tree
What is the printed output of the binary tree after inserting nodes 10, 5, 15 in that order using the given insert function?
DSA C
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
Node* insert(Node* root, int value) {
if (root == NULL) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
if (value < root->data) {
root->left = insert(root->left, value);
} else {
root->right = insert(root->right, value);
}
return root;
}
void printInOrder(Node* root) {
if (root == NULL) return;
printInOrder(root->left);
printf("%d -> ", root->data);
printInOrder(root->right);
}
int main() {
Node* root = NULL;
root = insert(root, 10);
root = insert(root, 5);
root = insert(root, 15);
printInOrder(root);
printf("null\n");
return 0;
}Attempts:
2 left
💡 Hint
In-order traversal prints nodes in ascending order for a binary search tree.
✗ Incorrect
The insert function places 5 to the left of 10 and 15 to the right. In-order traversal visits left subtree, root, then right subtree, so output is 5 -> 10 -> 15 -> null.
🧠 Conceptual
intermediateUnderstanding binary tree node pointers
In a binary tree node structure, what do the 'left' and 'right' pointers represent?
Attempts:
2 left
💡 Hint
Think about how a binary tree branches out from each node.
✗ Incorrect
In a binary tree, each node has up to two children. The 'left' pointer points to the left child node, and the 'right' pointer points to the right child node.
🔧 Debug
advancedIdentify the error in binary tree node initialization
What error will occur when running this code snippet for creating a new binary tree node?
DSA C
#include <stdlib.h> Node* createNode(int value) { Node* newNode = malloc(sizeof(Node)); newNode->data = value; newNode->left = NULL; newNode->right = NULL; return newNode; } int main() { Node* root = createNode(10); printf("%d\n", root->data); return 0; }
Attempts:
2 left
💡 Hint
malloc requires a specific header file to be included.
✗ Incorrect
The code uses malloc but does not include , which declares malloc. This causes a compilation error.
❓ Predict Output
advancedOutput of pre-order traversal on a binary tree
Given the binary tree constructed by inserting 8, 3, 10, 1, 6, 14, what is the output of the pre-order traversal?
DSA C
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
Node* insert(Node* root, int value) {
if (root == NULL) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
if (value < root->data) {
root->left = insert(root->left, value);
} else {
root->right = insert(root->right, value);
}
return root;
}
void printPreOrder(Node* root) {
if (root == NULL) return;
printf("%d -> ", root->data);
printPreOrder(root->left);
printPreOrder(root->right);
}
int main() {
Node* root = NULL;
int values[] = {8, 3, 10, 1, 6, 14};
for (int i = 0; i < 6; i++) {
root = insert(root, values[i]);
}
printPreOrder(root);
printf("null\n");
return 0;
}Attempts:
2 left
💡 Hint
Pre-order traversal visits root first, then left subtree, then right subtree.
✗ Incorrect
Pre-order traversal prints nodes in the order: root, left subtree, right subtree. The insertion creates the tree with root 8, left subtree 3->1,6 and right subtree 10->14.
🧠 Conceptual
expertMaximum nodes in a binary tree of height h
What is the maximum number of nodes in a binary tree of height h (where height is the number of edges on the longest path from root to leaf)?
Attempts:
2 left
💡 Hint
Think about a perfect binary tree where every level is fully filled.
✗ Incorrect
A perfect binary tree of height h has 2^(h+1) - 1 nodes because each level i has 2^i nodes, summing from i=0 to h.
