Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add the root node's value to the result list.
DSA C#
if (root == null) return new List<int>(); var result = new List<int>(); var queue = new Queue<TreeNode>(); queue.Enqueue(root); while (queue.Count > 0) { int levelSize = queue.Count; for (int i = 0; i < levelSize; i++) { var node = queue.Dequeue(); if (i == [1]) { result.Add(node.val); } if (node.left != null) queue.Enqueue(node.left); if (node.right != null) queue.Enqueue(node.right); } } return result;
Attempts:
3 left
💡 Hint
Common Mistakes
Adding the first node (index 0) instead of the last node.
Using levelSize instead of levelSize - 1 causing index errors.
✗ Incorrect
We add the node's value when i equals levelSize - 1 because that is the last node in the current level, representing the rightmost node visible from the right side.
2fill in blank
mediumComplete the code to enqueue the left child node if it exists.
DSA C#
if (node.left != null) [1];
Attempts:
3 left
💡 Hint
Common Mistakes
Enqueuing the right child instead of the left child.
Enqueuing the current node again causing infinite loops.
✗ Incorrect
We enqueue node.left to process the left child nodes in the next level.
3fill in blank
hardFix the error in the code to correctly initialize the queue with the root node.
DSA C#
var queue = new Queue<TreeNode>();
[1];Attempts:
3 left
💡 Hint
Common Mistakes
Enqueuing null or child nodes before the root.
Not enqueuing any node causing the loop to never run.
✗ Incorrect
We must enqueue the root node to start the level order traversal.
4fill in blank
hardFill both blanks to correctly check and enqueue the right child node.
DSA C#
if ([1] != null) [2];
Attempts:
3 left
💡 Hint
Common Mistakes
Checking the left child but enqueuing the right child or vice versa.
Enqueuing the left child twice.
✗ Incorrect
We check if node.right exists and enqueue it to process right child nodes.
5fill in blank
hardFill all three blanks to complete the method signature and return type for right side view.
DSA C#
public [1] [2](TreeNode [3]) { // method body }
Attempts:
3 left
💡 Hint
Common Mistakes
Using void as return type instead of List.
Wrong method name or parameter name.
✗ Incorrect
The method returns a list of integers, is named RightSideView, and takes root as parameter.
