Challenge - 5 Problems
Right Side View Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Right Side View for a Simple Binary Tree
What is the output of the right side view function for the given binary tree?
DSA C++
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
#include <vector>
#include <queue>
#include <iostream>
std::vector<int> rightSideView(TreeNode* root) {
std::vector<int> result;
if (!root) return result;
std::queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
int size = q.size();
for (int i = 0; i < size; ++i) {
TreeNode* node = q.front();
q.pop();
if (i == size - 1) result.push_back(node->val);
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
}
return result;
}
// Tree structure:
// 1
// / \
// 2 3
// \ \
// 5 4
int main() {
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);
root->left->right = new TreeNode(5);
root->right->right = new TreeNode(4);
std::vector<int> output = rightSideView(root);
for (int val : output) {
std::cout << val << " ";
}
return 0;
}Attempts:
2 left
💡 Hint
Think about which nodes are visible when looking from the right side level by level.
✗ Incorrect
The right side view shows the last node at each level when traversed from left to right. At level 1, node 1 is visible; at level 2, node 3; at level 3, node 4.
🧠 Conceptual
intermediate1:30remaining
Understanding the Right Side View Algorithm
Which of the following best describes the approach used to find the right side view of a binary tree?
Attempts:
2 left
💡 Hint
Consider which node is visible from the right side at each level.
✗ Incorrect
The right side view is obtained by recording the last node visited at each level during a level order traversal.
🔧 Debug
advanced2:00remaining
Identify the Error in Right Side View Implementation
What error will occur when running the following code snippet for right side view?
DSA C++
std::vector<int> rightSideView(TreeNode* root) { std::vector<int> result; std::queue<TreeNode*> q; q.push(root); while (!q.empty()) { int size = q.size(); for (int i = 0; i <= size; ++i) { TreeNode* node = q.front(); q.pop(); if (i == size - 1) result.push_back(node->val); if (node->left) q.push(node->left); if (node->right) q.push(node->right); } } return result; }
Attempts:
2 left
💡 Hint
Check the loop boundary condition carefully.
✗ Incorrect
The for loop uses i <= size which causes one extra iteration, leading to popping from an empty queue and runtime error.
❓ Predict Output
advanced2:00remaining
Output of Right Side View for Left-Skewed Tree
What is the output of the right side view function for this left-skewed binary tree?
DSA C++
// Tree structure: // 1 // / // 2 // / // 3 // #include <iostream> #include <vector> #include <queue> struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} }; std::vector<int> rightSideView(TreeNode* root) { std::vector<int> result; if (!root) return result; std::queue<TreeNode*> q; q.push(root); while (!q.empty()) { int size = q.size(); for (int i = 0; i < size; ++i) { TreeNode* node = q.front(); q.pop(); if (i == size - 1) result.push_back(node->val); if (node->left) q.push(node->left); if (node->right) q.push(node->right); } } return result; } int main() { TreeNode* root = new TreeNode(1); root->left = new TreeNode(2); root->left->left = new TreeNode(3); std::vector<int> output = rightSideView(root); for (int val : output) { std::cout << val << " "; } return 0; }
Attempts:
2 left
💡 Hint
Even if the tree is left-skewed, the right side view shows the rightmost node at each level.
✗ Incorrect
Since there are no right children, the right side view shows the only node at each level: 1, then 2, then 3.
🚀 Application
expert1:30remaining
Number of Nodes in Right Side View for Complete Binary Tree
Given a complete binary tree of height h (root at height 1), how many nodes will the right side view contain?
Attempts:
2 left
💡 Hint
Right side view shows one node per level.
✗ Incorrect
The right side view contains exactly one node per level, so for height h, it contains h nodes.