Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a map that stores nodes by their vertical column index.
DSA C++
std::map<int, std::vector<int>> [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that does not reflect the purpose, like 'treeMap'.
Using an invalid variable name.
✗ Incorrect
The variable name 'verticalOrder' clearly represents the map storing nodes by their vertical columns.
2fill in blank
mediumComplete the code to push the current node's value into the vector for its column in the map.
DSA C++
verticalOrder[[1]].push_back(node->val); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'row' or 'level' which represent vertical depth, not horizontal position.
Using an undefined variable.
✗ Incorrect
We use the 'column' index to group nodes vertically in the map.
3fill in blank
hardFix the error in the queue push operation to add the left child with updated column and row.
DSA C++
if (node->left) q.push({node->left, [1], [2]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping row and column updates.
Increasing column for left child.
✗ Incorrect
For the left child, column decreases by 1 and row increases by 1.
4fill in blank
hardFill both blanks to sort nodes first by row, then by value within each column.
DSA C++
std::sort(verticalOrder[[1]].begin(), verticalOrder[[2]].end());
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different keys for begin() and end().
Sorting by row which is not stored in the vector.
✗ Incorrect
We sort the vector of nodes in each column, so both blanks use 'column' as the key.
5fill in blank
hardFill all three blanks to build the final result vector from the map.
DSA C++
for (auto& [1] : verticalOrder) { result.push_back([2].second); } return [3];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names in the loop.
Returning the map instead of the result vector.
✗ Incorrect
We iterate over each columnPair in verticalOrder, push its vector to result, and return result.