Challenge - 5 Problems
Right Side View Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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)); } }
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. Level 1: 1, Level 2: 3, Level 3: 4.
🧠 Conceptual
intermediateUnderstanding the Right Side View Concept
Which statement best describes the right side view of a binary tree?
Attempts:
2 left
💡 Hint
Focus on what 'right side view' means visually.
✗ Incorrect
The right side view includes the rightmost node visible at each level when looking from the right side.
🔧 Debug
advancedIdentify 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;
}Attempts:
2 left
💡 Hint
Check the for loop condition and what happens when queue.poll() returns null.
✗ Incorrect
The for loop runs one extra time (i <= size), causing queue.poll() to return null, then accessing node.left causes NullPointerException.
🚀 Application
advancedRight 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);
Attempts:
2 left
💡 Hint
In a skewed tree with only left children, the right side view shows all nodes.
✗ Incorrect
Since there are no right children, the right side view shows all nodes from top to bottom.
❓ Predict Output
expertOutput 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)); } }
Attempts:
2 left
💡 Hint
The DFS visits right child first, adding nodes at new depths only once.
✗ Incorrect
The DFS adds the first node it sees at each depth, prioritizing right children, so the right side view is [1, 3, 6].
