Right Side View of Binary Tree in DSA Java - Time & Space Complexity
We want to understand how the time needed to find the right side view of a binary tree changes as the tree grows.
Specifically, how does the number of nodes affect the work done?
Analyze the time complexity of the following code snippet.
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 (i == size - 1) result.add(node.val);
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}
}
return result;
}
This code finds the right side view of a binary tree by visiting nodes level by level and recording the last node at each level.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Visiting each node once in a breadth-first manner using a queue.
- How many times: Each node is processed exactly one time.
As the number of nodes increases, the code visits each node once, so the work grows directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: The work grows linearly as the tree size grows.
Time Complexity: O(n)
This means the time to find the right side view grows directly with the number of nodes in the tree.
[X] Wrong: "Since we only take one node per level, the time complexity is O(h), where h is the tree height."
[OK] Correct: Even though we record one node per level, we still visit all nodes to find those rightmost nodes, so the time depends on total nodes, not just height.
Understanding this helps you explain how tree traversal works and how to analyze algorithms that visit all nodes efficiently.
"What if we changed the traversal to depth-first search instead of breadth-first search? How would the time complexity change?"
Practice
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 AQuick Check:
Right side view = rightmost nodes [OK]
- Confusing right side view with left side view
- Thinking it shows all leaf nodes
- Assuming it shows all nodes at deepest level
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 CQuick Check:
Return list of integers = public List<Integer> rightSideView(TreeNode root) [OK]
- Returning int instead of list
- Using void return type
- Incorrect parameter types
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));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 DQuick Check:
Rightmost nodes per level = [1, 3, 4] [OK]
- Choosing leftmost nodes instead of rightmost
- Adding all nodes instead of only rightmost
- Confusing order of nodes
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;
}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 BQuick Check:
Loop boundary off by one = The for loop condition should be i < size, not i <= size [OK]
- Using <= instead of < in loop condition
- Replacing queue with stack incorrectly
- Ignoring null root check
- Using Set instead of List unnecessarily
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 AQuick Check:
Multiple valid methods = All of the above approaches can achieve the reverse right side view [OK]
- Thinking only one method works
- Ignoring simple list reversal
- Confusing traversal order
Ready to prove it?
Pass the test and this pattern moves unknown
