Dynamic Programming21 sections · 916 units
Open in Course

LeetCode 968 Binary Tree Cameras - State Transitions

Three-state DP

state[v]{0,1,2}state[v] \in \{0, 1, 2\}: 00=has camera, 11=covered (no camera), 22=not covered (needs parent's camera). If any child is not covered (state 22), vv must have a camera (state 00).

If any child has camera (state 00), vv is covered (state 11). Otherwise, all children are covered (state 11), so vv is not covered (state 22) and relies on its parent. The three states capture all possibilities. The transitions make sure every node gets covered without redundant cameras.