0
0
DSA C++programming~20 mins

Right Side View of Binary Tree in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Right Side View Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
A[1, 3, 4]
B[1, 2, 5]
C[1, 3, 5]
D[1, 2, 4]
Attempts:
2 left
💡 Hint
Think about which nodes are visible when looking from the right side level by level.
🧠 Conceptual
intermediate
1: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?
ATraverse the tree level by level and record the last node at each level.
BTraverse the tree level by level and record the first node at each level.
CTraverse the tree in preorder and record all right children.
DTraverse the tree in inorder and record nodes with no left child.
Attempts:
2 left
💡 Hint
Consider which node is visible from the right side at each level.
🔧 Debug
advanced
2: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;
}
ALogic error: returns empty vector
BRuntime error due to popping from empty queue
CCompilation error due to missing semicolon
DNo error, returns correct output
Attempts:
2 left
💡 Hint
Check the loop boundary condition carefully.
Predict Output
advanced
2: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;
}
A[3]
B[1, 3]
C[1]
D[1, 2, 3]
Attempts:
2 left
💡 Hint
Even if the tree is left-skewed, the right side view shows the rightmost node at each level.
🚀 Application
expert
1: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?
Ah - 1
B2^h - 1
Ch
D2^(h-1)
Attempts:
2 left
💡 Hint
Right side view shows one node per level.