> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Binary Tree Cameras Python Solution with Tests

> Tested Python solution for LeetCode 968 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 968, [Hard](/catalog/hard). Topics: [Dynamic Programming](/catalog/topics/dynamic-programming), [Tree](/catalog/topics/tree), [Depth-First Search](/catalog/topics/depth-first-search), [Binary Tree](/catalog/topics/binary-tree), DP on Trees. [View on LeetCode](https://leetcode.com/problems/binary-tree-cameras/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 968   # by problem number
lcpy gen -s binary_tree_cameras   # by problem name
```

## 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](https://assets.leetcode.com/uploads/2018/12/29/bst_cameras_01.png)

```
Input: root = [0,0,null,0,0]
Output: 1
Explanation: One camera is enough to monitor all nodes if placed as shown.
```

![Example 2](https://assets.leetcode.com/uploads/2018/12/29/bst_cameras_02.png)

```
Input: root = [0,0,null,0,null,0,null,null,0]
Output: 2
Explanation: At least two cameras are needed to monitor all nodes of the tree. The above image shows one of the valid configurations of camera placement.
```

### 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](https://github.com/wislertt/leetcode-py/blob/main/leetcode/binary_tree_cameras/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/binary_tree_cameras/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from leetcode_py import TreeNode


class Solution:
    # Time: O(n)
    # Space: O(h) for the recursion stack
    def min_camera_cover(self, root: TreeNode[int] | None) -> int:
        cameras = 0

        # Post-order status per subtree: 0 needs a camera, 1 covered, 2 holds a camera
        def dfs(node: TreeNode[int] | None) -> int:
            nonlocal cameras
            if node is None:
                return 1
            left = dfs(node.left)
            right = dfs(node.right)
            if left == 0 or right == 0:
                cameras += 1
                return 2
            if left == 2 or right == 2:
                return 1
            return 0

        if dfs(root) == 0:
            cameras += 1
        return cameras
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(h) for the recursion stack |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.