Binary Tree Cameras

Hard

Topics
TreeDFSDynamic ProgrammingGreedy

You are given the root of a binary tree. A camera placed on a node can monitor its parent, itself, and its immediate children. Return the minimum number of cameras needed to monitor every node of the tree.

Example 1

Input:  root = [0,0,null,0,0]
Output: 1

Example 2

Input:  root = [0,0,null,0,null,0,null,null,0]
Output: 2

Constraints

  • 1 <= number of nodes <= 1000
  • Node.val == 0.
Run ⌘' · Submit ⌘⏎