Challenge - 5 Problems
Binary Tree Node Counting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Node Count Function on a Small Tree
What is the output of the following C code that counts total nodes in a binary tree?
DSA C
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* left; struct Node* right; } Node; int countNodes(Node* root) { if (root == NULL) return 0; return 1 + countNodes(root->left) + countNodes(root->right); } Node* newNode(int data) { Node* node = (Node*)malloc(sizeof(Node)); node->data = data; node->left = NULL; node->right = NULL; return node; } int main() { Node* root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->left->right = newNode(5); printf("%d\n", countNodes(root)); return 0; }
Attempts:
2 left
💡 Hint
Count all nodes including root, left subtree, and right subtree recursively.
✗ Incorrect
The tree has nodes: 1 (root), 2 and 3 (children of root), and 4 and 5 (children of node 2). Total nodes = 5.
❓ Predict Output
intermediateCount Nodes in a Tree with Only Left Children
What will be printed by this code counting nodes in a binary tree where each node has only a left child?
DSA C
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* left; struct Node* right; } Node; int countNodes(Node* root) { if (root == NULL) return 0; return 1 + countNodes(root->left) + countNodes(root->right); } Node* newNode(int data) { Node* node = (Node*)malloc(sizeof(Node)); node->data = data; node->left = NULL; node->right = NULL; return node; } int main() { Node* root = newNode(10); root->left = newNode(20); root->left->left = newNode(30); root->left->left->left = newNode(40); printf("%d\n", countNodes(root)); return 0; }
Attempts:
2 left
💡 Hint
Count all nodes down the left side; no right children exist.
✗ Incorrect
The tree has nodes 10, 20, 30, and 40 linked only through left children. Total nodes = 4.
❓ Predict Output
advancedOutput of Count Nodes on a Full Binary Tree
What is the output of this code counting nodes in a full binary tree of height 2?
DSA C
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* left; struct Node* right; } Node; int countNodes(Node* root) { if (root == NULL) return 0; return 1 + countNodes(root->left) + countNodes(root->right); } Node* newNode(int data) { Node* node = (Node*)malloc(sizeof(Node)); node->data = data; node->left = NULL; node->right = NULL; return node; } int main() { Node* root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->left->right = newNode(5); root->right->left = newNode(6); root->right->right = newNode(7); printf("%d\n", countNodes(root)); return 0; }
Attempts:
2 left
💡 Hint
Full binary tree of height 2 has 7 nodes.
✗ Incorrect
The tree has 7 nodes: root (1), two children (2,3), and four grandchildren (4,5,6,7). Total nodes = 7.
🔧 Debug
advancedIdentify the Error in Node Counting Code
What error will this code produce when counting nodes in a binary tree?
DSA C
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* left; struct Node* right; } Node; int countNodes(Node* root) { if (root == NULL) return 0; return 1 + countNodes(root->left) + countNodes(root->right); } Node* newNode(int data) { Node* node = (Node*)malloc(sizeof(Node)); node->data = data; node->left = NULL; node->right = NULL; return node; } int main() { Node* root = NULL; printf("%d\n", countNodes(root->left)); return 0; }
Attempts:
2 left
💡 Hint
Check if root is NULL before accessing its members.
✗ Incorrect
root is NULL, so accessing root->left causes a segmentation fault (runtime error).
🧠 Conceptual
expertNumber of Nodes in a Perfect Binary Tree of Height h
What is the formula for the total number of nodes in a perfect binary tree of height h (where height is number of edges from root to leaf)?
Attempts:
2 left
💡 Hint
Perfect binary tree has all levels fully filled.
✗ Incorrect
The total nodes in a perfect binary tree of height h is 2^(h+1) - 1.
