You are given the root of a binary tree. A camera placed on a node monitors that node, its parent, and its immediate children.
Return the minimum number of cameras needed so that every node of the tree is monitored.
Function Signature
def min_camera_cover(root: list[int | None]) -> int:
Rules
-
The tree is given as a level-order list.
None
marks a missing child, children of a missing node are not listed, and trailing
None
entries may be omitted.
-
Every node value is
0
. Values do not affect the answer.
Constraints
-
The number of nodes is in the range
[1, 1000]
.
-
Node.val == 0
Examples
Input: root = [0, 0, None, 0, 0]
Output: 1
The root has one child, and that child has two leaf children. A camera on the middle node monitors the root above it, itself, and both leaves.
Input: root = [0, 0, None, 0, None, 0, None, 0]
Output: 2
The list describes a path of five nodes, each one the left child of the node above it. A camera monitors at most three nodes of a path, so one camera is not enough. Cameras on the second and fourth nodes from the top cover all five.
Input: root = [0]
Output: 1
A single node has no parent or child to hold a camera, so it needs its own.