Bird
Raised Fist0
DSA C++programming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the "Right Side View of Binary Tree" represent?
easy
A. Nodes at the deepest level of the tree
B. Nodes visible when the tree is viewed from the right side
C. All leaf nodes of the binary tree
D. Nodes visible when the tree is viewed from the left side

Solution

  1. Step 1: Understand the view perspective

    The right side view shows nodes visible when looking at the tree from the right side, ignoring nodes hidden behind others.
  2. Step 2: Differentiate from other views

    Unlike left side or leaf views, right side view specifically captures the rightmost nodes at each level.
  3. Final Answer:

    Nodes visible when the tree is viewed from the right side -> Option B
  4. Quick Check:

    Right side view = rightmost nodes [OK]
Hint: Right side view shows last node at each tree level [OK]
Common Mistakes:
  • Confusing right side view with left side view
  • Thinking it shows all leaf nodes
  • Assuming it shows deepest nodes only
2. Which of the following C++ code snippets correctly declares a function to get the right side view of a binary tree?
easy
A. vector<int> rightSideView(TreeNode* root);
B. void rightSideView(TreeNode root);
C. int rightSideView(TreeNode* root);
D. vector<int> rightSideView(TreeNode root);

Solution

  1. Step 1: Check function return type

    The right side view returns a list of node values, so vector<int> is appropriate.
  2. Step 2: Check parameter type

    Function should take a pointer to root node (TreeNode*), not a value copy.
  3. Final Answer:

    vector<int> rightSideView(TreeNode* root); -> Option A
  4. Quick Check:

    Return vector<int>, parameter TreeNode* [OK]
Hint: Use vector<int> return and TreeNode* parameter [OK]
Common Mistakes:
  • Passing TreeNode by value instead of pointer
  • Returning int instead of vector<int>
  • Using void return type incorrectly
3. Given the binary tree below, what is the right side view output?

    1
   / \
  2   3
   \   \
    5   4
medium
A. [1, 3, 4]
B. [1, 2, 5]
C. [1, 3, 5]
D. [1, 2, 4]

Solution

  1. Step 1: Identify rightmost nodes at each level

    Level 1: Node 1; Level 2: Node 3 (right child of 1); Level 3: Node 4 (right child of 3).
  2. Step 2: Collect nodes in order

    Right side view is [1, 3, 4].
  3. Final Answer:

    [1, 3, 4] -> Option A
  4. Quick Check:

    Rightmost nodes per level = [1, 3, 4] [OK]
Hint: Pick last node visible at each tree level from right [OK]
Common Mistakes:
  • Choosing left child nodes instead of rightmost
  • Including nodes hidden behind others
  • Mixing node order in output
4. Consider this C++ function snippet to get the right side view:

void dfs(TreeNode* node, int level, vector<int>& res) {
  if (!node) return;
  if (level == res.size()) res.push_back(node->val);
  dfs(node->left, level + 1, res);
  dfs(node->right, level + 1, res);
}
What is the main bug in this code?
medium
A. It does not check for null nodes
B. It uses pass-by-value for vector, causing no updates
C. It pushes duplicate values into result vector
D. It visits left child before right, so rightmost nodes may be missed

Solution

  1. Step 1: Analyze traversal order

    Function visits left child before right child, so first node at each level is leftmost, not rightmost.
  2. Step 2: Understand impact on right side view

    Right side view requires visiting right child first to capture rightmost nodes at each level.
  3. Final Answer:

    It visits left child before right, so rightmost nodes may be missed -> Option D
  4. Quick Check:

    Right side view needs right-first DFS [OK]
Hint: Visit right child before left for correct right side view [OK]
Common Mistakes:
  • Visiting left child first in DFS for right side view
  • Ignoring null node checks (though present here)
  • Passing vector by value instead of reference
5. You want to find the right side view of a binary tree but the tree is very deep and unbalanced. Which approach is best to avoid stack overflow and get the correct right side view?
hard
A. Use recursive DFS visiting right child first without depth limit
B. Use recursive DFS visiting left child first
C. Use iterative level order traversal with a queue, capturing last node at each level
D. Use inorder traversal and collect nodes

Solution

  1. Step 1: Consider recursion limits on deep trees

    Deep unbalanced trees risk stack overflow with recursion, so iterative methods are safer.
  2. Step 2: Choose traversal method for right side view

    Level order traversal with a queue visits nodes level by level; capturing last node per level gives right side view.
  3. Step 3: Evaluate other options

    Recursive DFS may cause stack overflow; inorder traversal does not capture right side view correctly.
  4. Final Answer:

    Use iterative level order traversal with a queue, capturing last node at each level -> Option C
  5. Quick Check:

    Iterative BFS with queue = safe & correct [OK]
Hint: Use BFS queue, pick last node per level to avoid recursion issues [OK]
Common Mistakes:
  • Using recursion on very deep trees causing stack overflow
  • Visiting left child first in DFS for right side view
  • Using inorder traversal which doesn't capture right side view
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard