Bird
Raised Fist0
DSA Javaprogramming~20 mins

Right Side View of Binary Tree in DSA Java - 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 for the given binary tree?
DSA Java
class TreeNode {
    int val;
    TreeNode left, right;
    TreeNode(int val) { this.val = val; }
}

public class Solution {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if (root == null) return result;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                if (node.left != null) queue.offer(node.left);
                if (node.right != null) queue.offer(node.right);
                if (i == size - 1) result.add(node.val);
            }
        }
        return result;
    }

    public static void main(String[] args) {
        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);
        Solution sol = new Solution();
        System.out.println(sol.rightSideView(root));
    }
}
A[1, 3, 5]
B[1, 2, 5]
C[1, 3, 4]
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:00remaining
Understanding the Right Side View Concept
Which statement best describes the right side view of a binary tree?
AIt contains the leftmost node at each level of the tree.
BIt contains the rightmost node at each level of the tree.
CIt contains all leaf nodes of the tree.
DIt contains nodes in the order they are visited in preorder traversal.
Attempts:
2 left
💡 Hint
Focus on what 'right side view' means visually.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Right Side View Implementation
What error will occur when running this code snippet for right side view?
DSA Java
public List<Integer> rightSideView(TreeNode root) {
    List<Integer> result = new ArrayList<>();
    Queue<TreeNode> queue = new LinkedList<>();
    queue.offer(root);
    while (!queue.isEmpty()) {
        int size = queue.size();
        for (int i = 0; i < size; i++) {
            TreeNode node = queue.poll();
            if (node.left != null) queue.offer(node.left);
            if (node.right != null) queue.offer(node.right);
            if (i == size - 1) result.add(node.val);
        }
    }
    return result;
}
ANullPointerException
BInfinite loop
CNo error, outputs correct right side view
DIndexOutOfBoundsException
Attempts:
2 left
💡 Hint
Check the for loop condition and what happens when queue.poll() returns null.
🚀 Application
advanced
1:30remaining
Right Side View for a Skewed Binary Tree
What is the right side view output for this skewed binary tree where each node has only a left child: 1 -> 2 -> 3 -> 4?
DSA Java
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.left.left = new TreeNode(3);
root.left.left.left = new TreeNode(4);
A[1, 2, 3, 4]
B[4]
C[1]
D[]
Attempts:
2 left
💡 Hint
In a skewed tree with only left children, the right side view shows all nodes.
❓ Predict Output
expert
2:30remaining
Output of Modified Right Side View with Depth Tracking
What is the output of this modified right side view code that uses depth-first search?
DSA Java
class TreeNode {
    int val;
    TreeNode left, right;
    TreeNode(int val) { this.val = val; }
}

public class Solution {
    private List<Integer> result = new ArrayList<>();
    public List<Integer> rightSideView(TreeNode root) {
        dfs(root, 0);
        return result;
    }
    private void dfs(TreeNode node, int depth) {
        if (node == null) return;
        if (depth == result.size()) {
            result.add(node.val);
        }
        dfs(node.right, depth + 1);
        dfs(node.left, depth + 1);
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.left = new TreeNode(4);
        root.left.right = new TreeNode(5);
        root.right.right = new TreeNode(6);
        Solution sol = new Solution();
        System.out.println(sol.rightSideView(root));
    }
}
A[1, 3, 5, 6]
B[1, 2, 4, 5, 6]
C[1, 3, 4, 6]
D[1, 3, 6]
Attempts:
2 left
💡 Hint
The DFS visits right child first, adding nodes at new depths only once.

Practice

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

Solution

  1. Step 1: Understand the view perspective

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

    Unlike left side or leaf nodes, right side view includes the rightmost node at each level.
  3. Final Answer:

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

    Right side view = rightmost nodes [OK]
Hint: Right side view shows rightmost nodes at each level [OK]
Common Mistakes:
  • Confusing right side view with left side view
  • Thinking it shows all leaf nodes
  • Assuming it shows all nodes at deepest level
2. Which of the following Java method signatures is correct for a function that returns the right side view of a binary tree as a list of integers?
easy
A. public int rightSideView(TreeNode root)
B. public void rightSideView(TreeNode root)
C. public List<Integer> rightSideView(TreeNode root)
D. public TreeNode rightSideView(List<Integer> root)

Solution

  1. Step 1: Identify return type for right side view

    The right side view returns a list of integers representing node values visible from the right side.
  2. Step 2: Match method signature

    Method should return List<Integer> and accept TreeNode root as input.
  3. Final Answer:

    public List<Integer> rightSideView(TreeNode root) -> Option C
  4. Quick Check:

    Return list of integers = public List<Integer> rightSideView(TreeNode root) [OK]
Hint: Return a list of node values, input is root node [OK]
Common Mistakes:
  • Returning int instead of list
  • Using void return type
  • Incorrect parameter types
3. What is the output of the following code snippet for the right side view of the binary tree?
class TreeNode {
  int val;
  TreeNode left, right;
  TreeNode(int x) { val = x; }
}

public List<Integer> rightSideView(TreeNode root) {
  List<Integer> result = new ArrayList<>();
  if (root == null) return result;
  Queue<TreeNode> queue = new LinkedList<>();
  queue.add(root);
  while (!queue.isEmpty()) {
    int size = queue.size();
    for (int i = 0; i < size; i++) {
      TreeNode node = queue.poll();
      if (node.left != null) queue.add(node.left);
      if (node.right != null) queue.add(node.right);
      if (i == size - 1) result.add(node.val);
    }
  }
  return result;
}

// Tree structure:
//      1
//     / \
//    2   3
//     \   \
//      5   4

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);
System.out.println(rightSideView(root));
medium
A. [1, 2, 4]
B. [1, 2, 5]
C. [1, 3, 5]
D. [1, 3, 4]

Solution

  1. Step 1: Traverse level by level and identify rightmost nodes

    Level 1: Node 1 (only node), Level 2: Nodes 2 and 3, rightmost is 3, Level 3: Nodes 5 and 4, rightmost is 4.
  2. Step 2: Collect rightmost nodes in order

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

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

    Rightmost nodes per level = [1, 3, 4] [OK]
Hint: Pick last node at each level during BFS [OK]
Common Mistakes:
  • Choosing leftmost nodes instead of rightmost
  • Adding all nodes instead of only rightmost
  • Confusing order of nodes
4. Identify the bug in the following code for computing the right side view of a binary tree:
public List<Integer> rightSideView(TreeNode root) {
  List<Integer> result = new ArrayList<>();
  if (root == null) return result;
  Queue<TreeNode> queue = new LinkedList<>();
  queue.add(root);
  while (!queue.isEmpty()) {
    int size = queue.size();
    for (int i = 0; i <= size; i++) {
      TreeNode node = queue.poll();
      if (node.left != null) queue.add(node.left);
      if (node.right != null) queue.add(node.right);
      if (i == size - 1) result.add(node.val);
    }
  }
  return result;
}
medium
A. The null check for root is missing
B. The for loop condition should be i < size, not i <= size
C. The queue should be a Stack instead of a Queue
D. The result list should be a Set to avoid duplicates

Solution

  1. Step 1: Analyze the for loop condition

    The for loop runs from i = 0 to i <= size, which causes one extra iteration and poll from empty queue.
  2. Step 2: Correct the loop boundary

    The loop should run i < size to process exactly 'size' nodes per level.
  3. Final Answer:

    The for loop condition should be i < size, not i <= size -> Option B
  4. Quick Check:

    Loop boundary off by one = The for loop condition should be i < size, not i <= size [OK]
Hint: Use i < size for loops over queue size [OK]
Common Mistakes:
  • Using <= instead of < in loop condition
  • Replacing queue with stack incorrectly
  • Ignoring null root check
  • Using Set instead of List unnecessarily
5. Given a binary tree, how would you modify the standard right side view algorithm to return the right side view in reverse order (bottom to top)?
hard
A. All of the above approaches can achieve the reverse right side view
B. Traverse the tree from bottom to top using recursion and add rightmost nodes
C. Use a stack to store rightmost nodes during BFS and pop them to reverse order
D. Collect rightmost nodes top to bottom, then reverse the list before returning

Solution

  1. Step 1: Understand the standard right side view

    Standard approach collects rightmost nodes level by level from top to bottom.
  2. Step 2: Consider ways to reverse order

    We can reverse the collected list, use recursion bottom-up, or use a stack to reverse order after BFS.
  3. Step 3: Confirm all methods are valid

    Each method correctly produces the right side view in reverse order.
  4. Final Answer:

    All of the above approaches can achieve the reverse right side view -> Option A
  5. Quick Check:

    Multiple valid methods = All of the above approaches can achieve the reverse right side view [OK]
Hint: Reverse collected list or use stack/recursion [OK]
Common Mistakes:
  • Thinking only one method works
  • Ignoring simple list reversal
  • Confusing traversal order
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