Challenge - 5 Problems
Top View Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Top View for a Simple Binary Tree
What is the printed top view of the binary tree after running the given code?
DSA C
/* Binary Tree Structure: 1 / \ 2 3 \ \ 4 5 */ #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; } // Function to print top view void topView(struct Node* root) { if (!root) return; int hd = 0; int min_hd = 0, max_hd = 0; // For simplicity, assume horizontal distance range is -10 to 10 int top[21] = {0}; int visited[21] = {0}; // Queue for level order traversal struct Node* queue[100]; int hdQueue[100]; int front = 0, rear = 0; queue[rear] = root; hdQueue[rear] = 10; // offset 10 for index rear++; while (front < rear) { struct Node* temp = queue[front]; int hd = hdQueue[front]; front++; if (!visited[hd]) { top[hd] = temp->data; visited[hd] = 1; if (hd < min_hd) min_hd = hd; if (hd > max_hd) max_hd = hd; } if (temp->left) { queue[rear] = temp->left; hdQueue[rear] = hd - 1; rear++; } if (temp->right) { queue[rear] = temp->right; hdQueue[rear] = hd + 1; rear++; } } for (int i = 0; i <= 20; i++) { if (visited[i]) { printf("%d ", top[i]); } } printf("\n"); } int main() { struct Node* root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->right = newNode(4); root->right->right = newNode(5); topView(root); return 0; }
Attempts:
2 left
💡 Hint
Think about which nodes are visible from the top when looking horizontally.
✗ Incorrect
The top view includes nodes 2, 1, 3, and 5 because node 4 is hidden behind node 2 when viewed from the top.
❓ Predict Output
intermediateTop View Output for a Left Skewed Tree
What is the output of the top view function for this left skewed binary tree?
DSA C
/* Binary Tree Structure: 10 / 20 / 30 */ #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 topView(struct Node* root) { if (!root) return; int hd = 0; int min_hd = 0, max_hd = 0; int top[21] = {0}; int visited[21] = {0}; struct Node* queue[100]; int hdQueue[100]; int front = 0, rear = 0; queue[rear] = root; hdQueue[rear] = 10; rear++; while (front < rear) { struct Node* temp = queue[front]; int hd = hdQueue[front]; front++; if (!visited[hd]) { top[hd] = temp->data; visited[hd] = 1; if (hd < min_hd) min_hd = hd; if (hd > max_hd) max_hd = hd; } if (temp->left) { queue[rear] = temp->left; hdQueue[rear] = hd - 1; rear++; } if (temp->right) { queue[rear] = temp->right; hdQueue[rear] = hd + 1; rear++; } } for (int i = 0; i <= 20; i++) { if (visited[i]) { printf("%d ", top[i]); } } printf("\n"); } int main() { struct Node* root = newNode(10); root->left = newNode(20); root->left->left = newNode(30); topView(root); return 0; }
Attempts:
2 left
💡 Hint
In a left skewed tree, all nodes have decreasing horizontal distance.
✗ Incorrect
The top view shows all nodes because each has a unique horizontal distance: 30 at hd=8, 20 at hd=9, 10 at hd=10.
🔧 Debug
advancedIdentify the Error in Top View Implementation
What error will this code produce when running the top view function on a binary tree?
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 topView(struct Node* root) { if (!root) return; int top[5] = {0}; int visited[5] = {0}; struct Node* queue[100]; int hdQueue[100]; int front = 0, rear = 0; queue[rear] = root; hdQueue[rear] = 2; // center index rear++; while (front < rear) { struct Node* temp = queue[front]; int hd = hdQueue[front]; front++; if (!visited[hd]) { top[hd] = temp->data; visited[hd] = 1; } if (temp->left) { queue[rear] = temp->left; hdQueue[rear] = hd - 1; rear++; } if (temp->right) { queue[rear] = temp->right; hdQueue[rear] = hd + 1; rear++; } } for (int i = 0; i < 5; i++) { if (visited[i]) { printf("%d ", top[i]); } } printf("\n"); } 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); topView(root); return 0; }
Attempts:
2 left
💡 Hint
Check the size of arrays and the range of horizontal distances used.
✗ Incorrect
The horizontal distance can go below 0 or above 4, but arrays top and visited have size 5, causing index out of bounds.
🧠 Conceptual
advancedUnderstanding Horizontal Distance in Top View
In the top view of a binary tree, what does the horizontal distance (hd) represent?
Attempts:
2 left
💡 Hint
Think about how we assign horizontal distances when traversing left or right.
✗ Incorrect
Horizontal distance is assigned by subtracting 1 when moving left and adding 1 when moving right from the root's hd=0.
🚀 Application
expertTop View Nodes Count for a Complex Tree
Given the binary tree below, how many nodes will be visible in the top view?
Tree structure:
10
/ \
20 30
/ \ \
40 50 60
\ \
70 80
Choose the correct count of nodes visible in the top view.
Attempts:
2 left
💡 Hint
Count unique horizontal distances with the topmost node at each distance.
✗ Incorrect
Top view nodes are 40 (hd=-2), 20 (hd=-1), 10 (hd=0), 30 (hd=1), 60 (hd=2), 80 (hd=3). Total 6 nodes.
