Challenge - 5 Problems
Binary Tree Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Manual Binary Tree Creation and Inorder Traversal
What is the output of the inorder traversal after manually creating this binary tree?
DSA C++
struct Node {
int data;
Node* left;
Node* right;
Node(int val) : data(val), left(nullptr), right(nullptr) {}
};
Node* root = new Node(10);
root->left = new Node(5);
root->right = new Node(15);
root->left->left = new Node(3);
root->left->right = new Node(7);
void inorder(Node* node) {
if (!node) return;
inorder(node->left);
std::cout << node->data << " ";
inorder(node->right);
}
inorder(root);Attempts:
2 left
💡 Hint
Inorder traversal visits left subtree, then node, then right subtree.
✗ Incorrect
Inorder traversal visits nodes in ascending order for a binary search tree. The tree nodes are 3, 5, 7, 10, 15 in that order.
🧠 Conceptual
intermediate1:00remaining
Number of Nodes in a Manually Created Binary Tree
If you manually create a binary tree with root node and add two children to the root, and then add one child to the left child, how many nodes does the tree have?
Attempts:
2 left
💡 Hint
Count the root, its two children, and the one child added to the left child.
✗ Incorrect
The tree has 1 root + 2 children + 1 child of left child = 4 nodes total.
🔧 Debug
advanced2:00remaining
Identify the Bug in Manual Binary Tree Node Linking
What error will occur when running this code snippet that manually creates a binary tree?
DSA C++
struct Node {
int data;
Node* left;
Node* right;
Node(int val) : data(val), left(nullptr), right(nullptr) {}
};
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
// Mistake: Trying to access right child of a nullptr
std::cout << root->right->left->data << std::endl;Attempts:
2 left
💡 Hint
Check if root->right->left is initialized before accessing.
✗ Incorrect
root->right->left is nullptr because no node was assigned. Accessing its data causes a segmentation fault.
🚀 Application
advanced2:00remaining
Manual Binary Tree Height Calculation
Given a manually created binary tree, what is the height of the tree?
DSA C++
struct Node {
int data;
Node* left;
Node* right;
Node(int val) : data(val), left(nullptr), right(nullptr) {}
};
Node* root = new Node(8);
root->left = new Node(3);
root->right = new Node(10);
root->left->left = new Node(1);
root->left->right = new Node(6);
root->left->right->left = new Node(4);
root->right->right = new Node(14);
root->right->right->left = new Node(13);
int height(Node* node) {
if (!node) return 0;
int leftHeight = height(node->left);
int rightHeight = height(node->right);
return 1 + (leftHeight > rightHeight ? leftHeight : rightHeight);
}
int h = height(root);
std::cout << h << std::endl;Attempts:
2 left
💡 Hint
Height is the longest path from root to leaf node.
✗ Incorrect
The longest path is root(8) -> right(10) -> right(14) -> left(13), which has 4 nodes, so height is 4.
🧠 Conceptual
expert1:30remaining
Number of Null Pointers in a Complete Binary Tree of Height 3
How many null pointers are there in a complete binary tree of height 3 (height counted as number of nodes on longest path from root to leaf)?
Attempts:
2 left
💡 Hint
A complete binary tree of height h has 2^h - 1 nodes. Number of null pointers = number of missing children.
✗ Incorrect
A complete binary tree of height 3 has 7 nodes. Each node has 2 pointers, total 14 pointers. Since 7 nodes have 6 edges, null pointers = 14 - 6 = 8.