📖 Scenario: Imagine you have a tall tree in your backyard. You want to see which branches you can see if you stand on the right side of the tree and look straight ahead. This project helps you find those branches in a computer program using a binary tree.
🎯 Goal: You will build a program that shows the nodes visible from the right side of a binary tree. The program will print the values of these nodes in order from top to bottom.
📋 What You'll Learn
Create a binary tree with specific nodes
Use a variable to keep track of the current level during traversal
Write a function to find the right side view of the tree
Print the list of visible nodes from the right side
💡 Why This Matters
🌍 Real World
Right side view of a binary tree helps in visualizing hierarchical data from a specific angle, useful in graphics, UI layouts, and decision trees.
💼 Career
Understanding tree traversal and views is important for software engineers working with data structures, algorithms, and system design.
Progress0 / 4 steps
1
Create the Binary Tree
Create a class called TreeNode with an int val, and two TreeNode references called left and right. Then create a binary tree with root node value 1, left child 2, right child 3, left child of 2 as 4, and right child of 3 as 5. Store the root in a variable called root.
DSA Java
Hint
Start by defining the TreeNode class with the value and child nodes. Then create the nodes and link them as described.
2
Add a Level Tracker Variable
Inside the Main class, add a static integer variable called maxLevel and set it to 0. This will help track the deepest level visited during traversal.
DSA Java
Hint
Add the maxLevel variable as a static member of the Main class to keep track of the deepest level visited.
3
Write the Right Side View Function
Inside the Main class, write a static method called rightSideView that takes a TreeNode root and returns a List<Integer>. Use a helper method rightViewUtil(TreeNode node, int level, List<Integer> result) to traverse the tree from right to left. Add the node's value to result only if level > maxLevel, then update maxLevel. Call rightViewUtil starting with root and level 1. Return the result list.
DSA Java
Hint
Use a helper method to visit the right child first, then the left child. Add the node's value to the result list only if the current level is deeper than any level visited before.
4
Print the Right Side View
In the main method, call rightSideView(root) and store the result in a variable called rightView. Then print rightView.
DSA Java
Hint
Call the rightSideView method with root and print the returned list.
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
Step 1: Understand the view perspective
The right side view shows nodes visible when looking at the tree from the right side.
Step 2: Differentiate from other views
Unlike left side or leaf nodes, right side view includes the rightmost node at each level.
Final Answer:
Nodes visible when the tree is viewed from the right side -> Option A
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
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.
Step 2: Match method signature
Method should return List<Integer> and accept TreeNode root as input.
Final Answer:
public List<Integer> rightSideView(TreeNode root) -> Option C
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
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.
Step 2: Collect rightmost nodes in order
Right side view list is [1, 3, 4].
Final Answer:
[1, 3, 4] -> Option D
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
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.
Step 2: Correct the loop boundary
The loop should run i < size to process exactly 'size' nodes per level.
Final Answer:
The for loop condition should be i < size, not i <= size -> Option B
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
Step 1: Understand the standard right side view
Standard approach collects rightmost nodes level by level from top to bottom.
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.
Step 3: Confirm all methods are valid
Each method correctly produces the right side view in reverse order.
Final Answer:
All of the above approaches can achieve the reverse right side view -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.