Skip to content
Learn Netverks
Company prep Amazon
Mid-level (3–5 years) Coding / DSA Hard

Given a binary tree, place cameras to monitor all nodes

Tree DP pattern frequently reported in Amazon SDE-2 coding rounds.

Role
SDE-2
Location
Hyderabad
Study track
Java

Follow-up: optimize for minimum cameras. Expect discussion of post-order traversal and state machine on nodes.

Try answering aloud first

Cover trade-offs, structure, and a concrete example before revealing the baseline response.

Spoiler-free prep mode

Use post-order DFS with three states per node:

  1. Uncovered — subtree needs a camera above or at this node.
  2. Covered — subtree monitored, no camera needed here.
  3. Has camera — camera placed on this node.

Transition: if any child is uncovered, place camera on current node. If any child has camera, mark current covered. Else mark uncovered.

Time O(n), space O(h) for recursion stack. Mention iterative stack if interviewer prefers.

Comments (0)

Share how this question came up in your loop, or add tips for others preparing.

Log in to comment on this question.