Skip to main content
LeetCode 968, Hard. Topics: Dynamic Programming, Tree, Depth-First Search, Binary Tree, DP on Trees. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 20 parametrized pytest cases, and a playground notebook:

Problem

You are given the root of a binary tree. We install cameras on the tree nodes where each camera at a node can monitor its parent, itself, and its immediate children. Return the minimum number of cameras needed to monitor all nodes of the tree.

Examples

Example 1
Example 2

Constraints

  • The number of nodes in the tree is in the range [1, 1000]
  • Node.val == 0

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Last modified on September 7, 2026