Practice
Solution
Step 1: Identify camera placement logic
When a child is NOT_COVERED, a camera must be placed at current node and dfs must return HAS_CAM to indicate camera presence.Step 2: Locate incorrect return
The code returns COVERED_NO_CAM after placing a camera, which falsely signals no camera here, causing parents to misinterpret coverage.Final Answer:
Option A -> Option AQuick Check:
Returning HAS_CAM is essential after placing a camera [OK]
- Returning COVERED_NO_CAM instead of HAS_CAM after camera placement
- Forgetting to add camera if root uncovered
- Mixing coverage states in conditions
Solution
Step 1: Identify auxiliary space usage in recursion
The algorithm uses recursion, so the call stack depth is proportional to the height h of the tree.Step 2: Distinguish between in-place operations and recursion stack
Swapping pointers is in-place (O(1) per node), but recursion stack space is O(h), not O(n).Final Answer:
Option D -> Option DQuick Check:
Recursion stack dominates space, proportional to tree height h [OK]
- Confusing in-place operation with O(1) total space
- Assuming queue-based BFS space applies to recursive DFS
- Counting node swaps as extra space
Solution
Step 1: Understand coverage radius increase
Cameras now cover nodes up to distance 2, so coverage states must reflect grandchildren coverage, not just immediate children.Step 2: Adjust algorithm states
The original three states are insufficient; DFS must track coverage at a larger radius, increasing complexity and requiring more nuanced state management.Step 3: Evaluate options
Simply adding a fourth state (A) is vague and insufficient without redesign. Greedy placement without state changes (B) fails minimality. Brute force (D) is inefficient and unnecessary.Final Answer:
Option C -> Option CQuick Check:
Expanded coverage radius requires more complex state tracking [OK]
- Assuming original states suffice for extended coverage
- Relying on naive greedy placement
- Switching to brute force unnecessarily
Solution
Step 1: Understand traversal constraints
Without left/right pointers, standard Morris or recursion is not applicable; parent pointers only allow upward traversal.Step 2: Identify correct approach
Tracking previously visited nodes iteratively allows preorder traversal by moving up/down without extra space for recursion stack.Final Answer:
Option A -> Option AQuick Check:
Modified iterative approach handles parent-only trees without extra space [OK]
- Trying to use recursion without child pointers
- Attempting Morris traversal on parent pointers
- Using stack without tracking visited nodes
Solution
Step 1: Recognize diameter depends on path length (edges), not node values.
Negative values do not affect height or path length calculations.Step 2: Confirm standard height-based diameter algorithm works unchanged.
Heights and diameter calculations rely on structure, so no changes are needed.Final Answer:
Option A -> Option AQuick Check:
Diameter depends on edges, not node values [OK]
- Confusing diameter with max sum path
- Ignoring negative nodes incorrectly
- Switching to BFS unnecessarily
