0:00 / 0:00

Fewest cameras to watch every node: work up from the leaves with three states, put a camera on any parent of an unwatched child.

▼

The problem

LeetCode 968 (Hard). Install cameras on the nodes of a binary tree. Each camera monitors its parent, itself and its immediate children. Return the minimum number of cameras needed to monitor every node.

Examples (LeetCode's): [0,0,null,0,0] → 1 (one camera on the root's child) and [0,0,null,0,null,0,null,null,0] (a chain of five) → 2.

TRY IT ON LEETCODE ▶

The solution

def minCameraCover(root):
    cams = 0
    def dfs(node):              # post-order
        nonlocal cams
        if not node:
            return 2            # missing: watched
        left, right = dfs(node.left), dfs(node.right)
        if left == 0 or right == 0:
            cams += 1
            return 1            # camera here
        if left == 1 or right == 1:
            return 2            # watched by a child
        return 0                # parent covers it
    if dfs(root) == 0:
        cams += 1               # fix the root
    return cams

Transcript

Binary Tree Cameras. Put cameras on the nodes of a binary tree. A camera watches its node, its parent and its children. Return the fewest cameras that watch every node. Each node here is a museum room.

A root, one child, two grandchildren: one camera on the child sees all four. Five rooms in a chain need two, since one camera sees at most three.

The simple way tries every set of camera rooms and keeps the smallest that watches everything. That's two to the n sets, each checked in n steps. Thirty rooms means over a billion sets.

The key idea: never put a camera on a leaf. There, it watches just two rooms. One room up, on the leaf's parent, it watches all four. So work from the bottom up.

Each room reports a state to its parent. Zero: not watched. One: has a camera. Two: watched, without a camera. A missing child counts as two, so every leaf reports zero.

Three rules, children first. If any child is zero, put a camera here: report one. Else, if any child has a camera, report two. Otherwise, report zero, and leave it to the parent. Up the chain: zero, one, two, zero, one. Two cameras.

In code, a post-order helper returns each state and counts cameras. One catch: the root has no parent. If it ends at zero, add one more camera.

Let's try eight rooms. The three leaves report zero, so their parents get cameras. One level up, both rooms see a camera and report two. The root sees only twos, so it reports zero. Nothing is above it, so it takes the third camera. Brute force agrees: three.

Each room is visited once: order n time. The recursion goes as deep as the tree: order h space, where h is the height.

Never on a leaf, three states, fix the root. That's Binary Tree Cameras.