Challenge - 5 Problems
Binary Tree Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Manual Binary Tree Creation
What is the printed output of this C code that creates a binary tree manually and prints its inorder traversal?
DSA C
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* left; struct Node* right; }; struct Node* newNode(int data) { struct Node* node = (struct Node*)malloc(sizeof(struct Node)); node->data = data; node->left = NULL; node->right = NULL; return node; } void inorder(struct Node* root) { if (root != NULL) { inorder(root->left); printf("%d ", root->data); inorder(root->right); } } int main() { struct Node* root = newNode(10); root->left = newNode(5); root->right = newNode(15); root->left->left = newNode(3); root->left->right = newNode(7); inorder(root); return 0; }
Attempts:
2 left
💡 Hint
Inorder traversal prints left subtree, root, then right subtree.
✗ Incorrect
The inorder traversal visits nodes in ascending order for a binary search tree. The tree nodes are 3, 5, 7, 10, 15 arranged so that inorder prints them sorted.
❓ Predict Output
intermediateOutput of Preorder Traversal on Manual Binary Tree
What is the output of the preorder traversal of this manually created binary tree in C?
DSA C
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* left; struct Node* right; }; struct Node* newNode(int data) { struct Node* node = (struct Node*)malloc(sizeof(struct Node)); node->data = data; node->left = NULL; node->right = NULL; return node; } void preorder(struct Node* root) { if (root != NULL) { printf("%d ", root->data); preorder(root->left); preorder(root->right); } } int main() { struct Node* root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->left->right = newNode(5); preorder(root); return 0; }
Attempts:
2 left
💡 Hint
Preorder traversal visits root first, then left subtree, then right subtree.
✗ Incorrect
Preorder traversal prints nodes in the order: root, left subtree, right subtree. The tree structure leads to output 1 2 4 5 3.
🔧 Debug
advancedIdentify the Error in Manual Binary Tree Creation
What error does this C code produce when trying to create a binary tree manually?
DSA C
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* left; struct Node* right; }; struct Node* newNode(int data) { struct Node* node = malloc(sizeof(struct Node)); node->data = data; node->left = NULL; node->right = NULL; return node; } int main() { struct Node* root = newNode(10); root->left = newNode(5); root->right = newNode(15); root->left->left = newNode(3); root->left->right = newNode(7); printf("%d", root->left->left->data); return 0; }
Attempts:
2 left
💡 Hint
Casting malloc in C is optional and allowed. The code includes stdlib.h.
✗ Incorrect
The code correctly creates nodes and prints the data 3. Casting malloc is optional in C. The code compiles and runs without error.
🧠 Conceptual
advancedNumber of Nodes in a Manually Created Binary Tree
If you manually create a binary tree in C with root, root->left, root->right, root->left->left, and root->left->right nodes, how many nodes does the tree have?
Attempts:
2 left
💡 Hint
Count each node created explicitly.
✗ Incorrect
The tree has 5 nodes: root, root->left, root->right, root->left->left, and root->left->right.
🚀 Application
expertManual Binary Tree Height Calculation
Given this manually created binary tree in C, what is the height of the tree? Height is the number of edges on the longest path from root to a leaf.
DSA C
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* left; struct Node* right; }; struct Node* newNode(int data) { struct Node* node = (struct Node*)malloc(sizeof(struct Node)); node->data = data; node->left = NULL; node->right = NULL; return node; } int height(struct Node* root) { if (root == NULL) return -1; int leftHeight = height(root->left); int rightHeight = height(root->right); return (leftHeight > rightHeight ? leftHeight : rightHeight) + 1; } int main() { struct Node* root = newNode(20); root->left = newNode(10); root->right = newNode(30); root->left->left = newNode(5); root->left->left->left = newNode(2); root->right->right = newNode(40); printf("%d", height(root)); return 0; }
Attempts:
2 left
💡 Hint
Count edges on longest path from root to leaf.
✗ Incorrect
The longest path is from root (20) to 10 to 5 to 2, which has 3 edges, so height is 3.
