Find the fewest cameras that monitor every node of a binary tree

Quick Overview

Find the minimum number of cameras needed to monitor every node of a binary tree, where a camera watches its own node, its parent and its children. It tests tree traversal, greedy decisions with a proof that they are optimal, and careful handling of leaves and the root.

Find the fewest cameras that monitor every node of a binary tree

Company: eBay

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

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 ```python 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 ```text 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. ```text 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. ```text Input: root = [0] Output: 1 ``` A single node has no parent or child to hold a camera, so it needs its own.

Overview: Find the minimum number of cameras needed to monitor every node of a binary tree, where a camera watches its own node, its parent and its children. It tests tree traversal, greedy decisions with a proof that they are optimal, and careful handling of leaves and the root.

eBay logo
eBay
Nov 24, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

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.