Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the method that returns the right side view list.
DSA Java
public List<Integer> [1](TreeNode root) { List<Integer> result = new ArrayList<>(); // method body return result; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'leftSideView' or other view names instead of 'rightSideView'.
✗ Incorrect
The method name should be 'rightSideView' to match the problem requirement.
2fill in blank
mediumComplete the code to add the rightmost node's value at each level during BFS.
DSA Java
while (!queue.isEmpty()) { int size = queue.size(); for (int i = 0; i < size; i++) { TreeNode node = queue.poll(); if (i == [1]) { result.add(node.val); } if (node.left != null) queue.add(node.left); if (node.right != null) queue.add(node.right); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or size as the index to add node value instead of size - 1.
✗ Incorrect
The rightmost node at each level is at index size - 1 in the loop.
3fill in blank
hardFix the error in the code that adds child nodes to the queue in BFS.
DSA Java
if (node.left != null) queue.[1](node.left); if (node.right != null) queue.[1](node.right);
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'push' which is for stacks, or 'pop'/'peek' which remove or view elements.
✗ Incorrect
In Java Queue, 'add' is used to insert elements at the end.
4fill in blank
hardFill both blanks to complete the DFS helper method that tracks depth and updates the right side view list.
DSA Java
private void dfs(TreeNode node, int [1], List<Integer> result) { if (node == null) return; if ([2] == result.size()) { result.add(node.val); } dfs(node.right, depth + 1, result); dfs(node.left, depth + 1, result); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently for depth.
✗ Incorrect
The variable tracking current depth is commonly named 'depth' and is compared to result.size().
5fill in blank
hardFill all three blanks to complete the DFS call in the main method and return the result.
DSA Java
List<Integer> result = new ArrayList<>(); dfs([1], [2], [3]); return result;
Attempts:
3 left
💡 Hint
Common Mistakes
Passing null or wrong initial depth.
✗ Incorrect
The DFS call starts with root node, depth 0, and the result list.
