Top View of Binary Tree in DSA C - Time & Space Complexity
We want to understand how the time needed to find the top view of a binary tree changes as the tree grows.
How does the number of nodes affect the work done to get the top view?
Analyze the time complexity of the following code snippet.
// Structure for tree node
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// Function to print top view
void topView(struct Node* root) {
if (!root) return;
// Use a queue for level order traversal
// Use a map to store first node at each horizontal distance
// Traverse tree and print top view nodes
}
This code performs a level order traversal to find the top view nodes of the binary tree.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Level order traversal visiting each node once.
- How many times: Each node is visited exactly one time.
As the number of nodes increases, the traversal visits each node once, so the work grows directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: The operations grow linearly with the number of nodes.
Time Complexity: O(n)
This means the time to find the top view grows in direct proportion to the number of nodes in the tree.
[X] Wrong: "The top view requires checking all paths multiple times, so it must be more than linear."
[OK] Correct: The level order traversal visits each node once, and the map stores only the first node at each horizontal distance, so no repeated work happens.
Understanding this helps you explain how tree traversals work efficiently and how to use extra data structures to track needed information.
"What if we used a recursive approach instead of level order traversal? How would the time complexity change?"
Practice
top view of a binary tree represent?Solution
Step 1: Understand the meaning of top view
The top view of a binary tree shows nodes visible when looking down vertically from above the tree.Step 2: Compare with other views
Left or right views show nodes from side angles, and leaf nodes are only the bottom nodes, not the top view.Final Answer:
Nodes visible when the tree is viewed from the top -> Option AQuick Check:
Top view = nodes seen from above [OK]
- Confusing top view with left or right view
- Thinking top view includes all leaf nodes
- Assuming top view shows all nodes
Solution
Step 1: Identify traversal method for top view
Top view is found using level order traversal, which uses a queue to process nodes level by level.Step 2: Understand role of queue
Queue helps visit nodes in order from top to bottom and left to right, needed for correct top view.Final Answer:
Queue -> Option BQuick Check:
Level order traversal uses queue [OK]
- Using stack which is for depth-first traversal
- Confusing hash map as traversal structure
- Using linked list instead of queue
1
/ \
2 3
\ \
4 5
\ \
6 7
Solution
Step 1: Assign horizontal distances
Assign horizontal distance (HD) from root: root(1) = 0, left child decreases HD by 1, right child increases HD by 1. So, 2 = -1, 3 = 1, 4 = 0, 5 = 2, 6 = 1, 7 = 3.Step 2: Select topmost nodes at each HD
At HD -1: 2; HD 0: 1 (4 is lower); HD 1: 3 (6 is lower); HD 2: 5; HD 3: 7.Final Answer:
[2, 1, 3, 5, 7] -> Option DQuick Check:
Top view nodes by HD = [2,1,3,5,7] [OK]
- Choosing lower nodes instead of top nodes
- Ignoring horizontal distance concept
- Mixing order of nodes in output
void topView(Node* root) {
if (!root) return;
map topNode;
queue> q;
q.push({root, 0});
while (!q.empty()) {
auto p = q.front(); q.pop();
Node* node = p.first;
int hd = p.second;
if (topNode.find(hd) == topNode.end())
topNode[hd] = node->data;
if (node->left) q.push({node->left, hd - 1});
if (node->right) q.push({node->right, hd + 1});
}
for (auto it : topNode)
cout << it.second << " ";
}
What is the error in this code?Solution
Step 1: Identify language mismatch
The code uses C++ STL containers like map and queue, but the function is declared in C style which does not support these STL features.Step 2: Check other code parts
Root NULL check is present, horizontal distance is correctly updated, and void functions do not require return statements.Final Answer:
Using C++ STL map and queue in C code -> Option CQuick Check:
C code cannot use C++ STL containers [OK]
- Ignoring language differences between C and C++
- Thinking void functions need return
- Assuming horizontal distance logic is wrong
Solution
Step 1: Understand node selection in top view
Top view shows the first node encountered at each horizontal distance during level order traversal, which naturally picks the leftmost node at that distance.Step 2: Avoid overwriting nodes
By storing only the first node at each horizontal distance and ignoring later nodes, the leftmost node is preserved.Final Answer:
Store the first node encountered at each horizontal distance during level order traversal -> Option AQuick Check:
First node at HD = leftmost node [OK]
- Overwriting with last node instead of first
- Choosing nodes by max depth instead of min
- Preferring right child first breaks leftmost rule
Ready to prove it?
Pass the test and this pattern moves unknown
