AlgoMaster Logo

Binary Tree Cameras

Last Updated: March 28, 2026

hard
3 min read

Understanding the Problem

We need to place the fewest cameras possible on a binary tree so that every node is "covered," meaning it either has a camera on it or is adjacent to a node with a camera. A camera covers the node it sits on, its parent, and its direct children.

This is a minimum vertex cover variant on trees, but with a twist: coverage extends to the parent and both children, not just neighbors in one direction. The challenge is figuring out which nodes benefit most from having a camera. Placing a camera on a leaf only covers two nodes (itself and its parent), but placing one on the leaf's parent covers up to four nodes (itself, both children, and its own parent). That observation is the key to the entire solution.

Key Constraints:

  • 1 <= number of nodes <= 1000 - The tree is small. Even O(n^2) would run in under a millisecond. But the real challenge here is correctness, not performance.
  • Node.val == 0 - The values do not matter. This is purely a structural problem about the tree's shape.

Approach 1: Brute Force (Try All Subsets)

Intuition

The most direct approach: try every possible subset of nodes to place cameras on, check if all nodes are covered, and return the size of the smallest valid subset.

For each node, we either place a camera or we do not. That gives us 2^n subsets. For each subset, we verify that every node is either a camera node or adjacent to one. This is conceptually simple but wildly inefficient.

Algorithm

  1. Enumerate all 2^n subsets of nodes.
  2. For each subset, mark the selected nodes as having cameras.
  3. Check if every node in the tree is covered (has a camera or is adjacent to a camera node).
  4. Track the minimum subset size among all valid configurations.
  5. Return that minimum.

Example Walkthrough

1Tree has 4 nodes. Try all 2^4 = 16 subsets of camera placements.
0000
1/4

Code

This approach is exponential and unusable for the given constraints. Can we make a local decision at each node using information from its children instead of trying all combinations?

Approach 2: Greedy DFS (Three States)

Intuition

Instead of trying all subsets, think about what each node needs to "report" to its parent. After processing both children, a node knows everything it needs to make a decision. Each node can be in exactly one of three states:

  • State 0 (NOT_COVERED): This node has no camera and is not covered by any child's camera. It needs its parent to place a camera.
  • State 1 (HAS_CAMERA): This node has a camera on it. It covers itself, its children, and its parent.
  • State 2 (COVERED): This node does not have a camera but is already covered by at least one child's camera.

Algorithm

  1. Define three states: NOT_COVERED = 0, HAS_CAMERA = 1, COVERED = 2.
  2. Run a post-order DFS on the tree.
  3. Treat null children as COVERED (they do not need monitoring and should not force a camera).
  4. At each node, apply the state transition rules based on children's states:
    • If any child is NOT_COVERED, place a camera on this node.
    • If any child HAS_CAMERA, this node is COVERED.
    • If both children are COVERED, this node is NOT_COVERED.
  5. After the DFS, if the root is NOT_COVERED, add one more camera for the root.
  6. Return the total camera count.

Example Walkthrough

1Initial tree. Process leaves first (post-order).
0000
1/6

Code