Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the vector that will store the right side view nodes.
DSA C++
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 is not declared or inconsistent with the rest of the code.
✗ Incorrect
The vector named 'result' is commonly used to store the right side view nodes in this implementation.
2fill in blank
mediumComplete the code to check if the current node is null before processing.
DSA C++
if ([1] == nullptr) return;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking a variable that is not the current node pointer.
✗ Incorrect
The parameter 'node' is used to represent the current node in the recursive function, so we check if 'node' is null.
3fill in blank
hardFix the error in the condition that adds the node's value to the result vector only if it's the first node at this level.
DSA C++
if (level == [1]) result.push_back(node->val);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing level with level itself or with node value instead of result size.
✗ Incorrect
We compare the current level with the size of the result vector to ensure only the first node at each level is added.
4fill in blank
hardFill both blanks to recursively traverse the right child first, then the left child.
DSA C++
dfs([1], level + 1, result); dfs([2], level + 1, result);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Visiting left child before right child, which gives wrong view.
✗ Incorrect
To get the right side view, we visit the right child first (node->right), then the left child (node->left).
5fill in blank
hardFill all three blanks to complete the main function that calls the helper and returns the right side view.
DSA C++
std::vector<int> rightSideView(TreeNode* root) {
std::vector<int> [1];
dfs([2], 0, [3]);
return [3];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names or forgetting to return the result vector.
✗ Incorrect
We declare 'result' vector, call dfs with root and level 0, then return 'result'.