Bird
Raised Fist0

Consider the optimal camera placement code. What is the output when the input tree is a single node (root only)?

medium🧾 Code Trace Q4 of Q15
Tree: Depth-First Search - Binary Tree Cameras
Consider the optimal camera placement code. What is the output when the input tree is a single node (root only)?
A1
B0
C2
Dnull (error due to missing children)
Step-by-Step Solution
Solution:
  1. Step 1: dfs on null children returns COVERED_NO_CAM

    Left and right children are null, so dfs returns COVERED_NO_CAM (1).
  2. Step 2: Both children covered but no camera, so root returns NOT_COVERED

    Since left and right are COVERED_NO_CAM, root returns NOT_COVERED (0).
  3. Step 3: After dfs, root is NOT_COVERED, so add one camera

    Increment cameras to 1.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Single node requires one camera [OK]
Quick Trick: Root uncovered after dfs triggers camera addition [OK]
Common Mistakes:
MISTAKES
  • Assuming no camera needed for single node
Trap Explanation:
PITFALL
  • Candidates forget to add camera if root remains uncovered after traversal.
Interviewer Note:
CONTEXT
  • Tests handling of minimal edge cases and root coverage logic
Master "Binary Tree Cameras" in Tree: Depth-First Search

3 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Tree: Depth-First Search Quizzes