0
0
DSA C++programming~10 mins

Vertical Order Traversal of Binary Tree in DSA C++ - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AverticalOrder
BnodeMap
CcolumnTable
DtreeMap
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that does not reflect the purpose, like 'treeMap'.
Using an invalid variable name.
2fill in blank
medium

Complete 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'
Arow
Bcolumn
Clevel
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'row' or 'level' which represent vertical depth, not horizontal position.
Using an undefined variable.
3fill in blank
hard

Fix 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'
Acolumn - 1
Brow + 1
Ccolumn + 1
Drow - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping row and column updates.
Increasing column for left child.
4fill in blank
hard

Fill 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'
Acolumn
Brow
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using different keys for begin() and end().
Sorting by row which is not stored in the vector.
5fill in blank
hard

Fill 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'
AcolumnPair
Cresult
DverticalOrder
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names in the loop.
Returning the map instead of the result vector.