Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the map that stores the bottom view nodes with their horizontal distance.
DSA C++
std::map<int, int> [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a map name related to top view instead of bottom view.
Using an unrelated variable name.
✗ Incorrect
The map named bottomViewMap stores the horizontal distance as key and node's data as value for bottom view.
2fill in blank
mediumComplete the code to insert or update the bottom view map with the current node's data at its horizontal distance.
DSA C++
bottomViewMap[[1]] = node->data; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using level or depth instead of horizontal distance as key.
Using an undefined variable.
✗ Incorrect
The horizontal distance is used as the key to store the node's data in the bottom view map.
3fill in blank
hardFix the error in the queue push operation to add the left child with updated horizontal distance.
DSA C++
if (node->left) q.push(std::make_pair(node->left, [1]));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Increasing horizontal distance for left child.
Using the same horizontal distance as parent.
✗ Incorrect
The left child is at horizontal distance one less than the current node, so horizontalDistance - 1 is correct.
4fill in blank
hardFill both blanks to correctly push the right child with updated horizontal distance and the current level incremented.
DSA C++
if (node->right) q.push(std::make_pair([1], [2]));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using left child instead of right child.
Not updating horizontal distance correctly.
✗ Incorrect
The right child node pointer and its horizontal distance (parent's + 1) are pushed into the queue.
5fill in blank
hardFill all three blanks to print the bottom view nodes in order of their horizontal distance.
DSA C++
for (auto [1] : bottomViewMap) { std::cout << [2] << " "; } std::cout << [3];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Printing the key (entry.first) instead of the node data.
Forgetting to print a newline at the end.
✗ Incorrect
We iterate over each entry in the map, print the node data (entry.second), and end with a newline (std::endl).