Problem Statement

Binary Tree Cameras

You have a binary tree. A binary tree is a set of dots called nodes, where each node can point down to a left child and a right child. You want to put cameras on some of the nodes. A camera sees three things: the node it sits on, that node's parent right above it, and that node's direct children right below it. The goal is to cover every single node using as few cameras as possible. The smart move is to work from the bottom up and put cameras on the parents of the leaf nodes. A leaf is a node with no children, the very bottom of the tree. We never waste a camera on a leaf itself, because a camera one level up covers the leaf, the leaf's parent, and that parent's other child all at once. We do this with a post-order DFS, which means we fully explore both children of a node before we make a decision about the node itself. This bottom-up greedy plan gives the smallest count.

hardTreeGreedyTreesTime: O(n) · Space: O(h)

Signals to notice

minimum cameras to monitor all nodesgreedy from leaves upwardthree states per node

Brute force first

Try all subsets of nodes for camera placement. That instinct is useful because it follows the prompt literally, but it usually keeps revisiting work the problem is begging you to organize.

The key insight

Post-order DFS with three states: 0 = needs monitoring, 1 = has camera, 2 = monitored by child. Leaves return 0 (need monitoring). If any child is 0, place camera (return 1). If any child is 1, this node is covered (return 2). Instead of recomputing the world every time, you preserve just enough context to let the next decision become obvious.

Trace it on tree=[0,0,null,0,0] (root R -> left M; M -> leaves L1,L2)

dfs(L1): node.left=null->2, node.right=null->2; no 0, no 1 -> return 0 (uncovered)
dfs(L2): same as L1 -> return 0 (uncovered)
dfs(M): L=0 -> place camera, cameras=1, return 1 (has camera)
dfs(R): L(M)=1, right=null->2; L==1 -> return 2 (covered by M's camera)
dfs(root)=2 (not 0) -> no extra camera at root
return cameras = 1

What must stay true

Placing cameras at parents of leaves is always optimal — a camera at a leaf only covers itself and its parent, while a camera at the parent covers the parent, leaf, and grandparent. As long as that statement keeps holding, you can trust the steps built on top of it.

Shape of the loop

function dfs(node):
  if node is null: return 2          # MONITORED (null can't be "uncovered")
  L, R = dfs(node.left), dfs(node.right)
  if L == 0 or R == 0: cameras++; return 1   # child needs cover -> place camera
  if L == 1 or R == 1: return 2              # a child has a camera -> we're covered
  return 0                                   # uncovered, parent must cover us
# after recursion: if dfs(root) == 0: cameras++

Pseudocode only — the full worked solution lives in the Solution tab.

Easy way to go wrong

Placing cameras at leaves — that's suboptimal. Always place cameras one level above the leaves for maximum coverage. The fix is usually to return to the meaning of each move, not just the steps themselves.

Trees Pattern