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.

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. ### Input format The tree is given as a level-order list `root`. `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`, and values do not affect the answer. Every language receives the same list. In JavaScript a missing child is `null`. In Java the list is a `java.util.List<Integer>` and a missing child is a `null` element. In C++ each entry arrives as a `std::string`: an entry holding a number is a node, and any other entry marks a missing child. ### Output Return the minimum number of cameras as an integer. It is between 1 and the number of nodes, so it is at most 1000 and never exceeds 2^31-1. ### Example 1 ```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. ### Example 2 ```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. ### Constraints - The number of nodes is in the range `[1, 1000]`. - `Node.val == 0`

Constraints

  • The number of nodes is in the range [1, 1000].
  • Node.val == 0 for every node.
  • root is a level-order list: None marks a missing child, children of a missing node are not listed, and trailing None entries may be omitted.

Examples

Input: ([0],)

Expected Output: 1

Explanation: Source example: a single node has no neighbor to hold a camera, so it needs its own.

Input: ([0, 0],)

Expected Output: 1

Explanation: Root with one left leaf: one camera on either node covers both.

Hints

  1. A camera on a leaf monitors only the leaf and its parent. Compare that with what a camera on the leaf's parent would monitor.
  2. Try settling a node only after everything below it is settled. What does a node need to know about each of its children?
  3. The root has no parent, so nothing above it can monitor it.

Fewest Cameras to Monitor a Binary Tree

hardeBaySoftware Engineer

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.

Input format

The tree is given as a level-order list root. 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, and values do not affect the answer.

Every language receives the same list. In JavaScript a missing child is null. In Java the list is a java.util.List<Integer> and a missing child is a null element. In C++ each entry arrives as a std::string: an entry holding a number is a node, and any other entry marks a missing child.

Output

Return the minimum number of cameras as an integer. It is between 1 and the number of nodes, so it is at most 1000 and never exceeds 2^31-1.

Example 1

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.

Example 2

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.

Constraints

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

Examples

Example 1
Input
([0],)
Output
1
Notes
Source example: a single node has no neighbor to hold a camera, so it needs its own.
Example 2
Input
([0, 0],)
Output
1
Notes
Root with one left leaf: one camera on either node covers both.

Constraints

  • •The number of nodes is in the range [1, 1000].
  • •Node.val == 0 for every node.
  • •root is a level-order list: None marks a missing child, children of a missing node are not listed, and trailing None entries may be omitted.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def min_camera_cover(root: list[str]) -> int:
Sample call
min_camera_cover([0])
Language
Case 1
Input
([0],)
Expected
1
Case 2
Input
([0, 0],)
Expected
1

Browse More Questions

Show the approach

Approach

Build the tree from the level-order list with a queue. Nodes are numbered in the order they appear, and each node, taken in that order, claims the next two list entries as its left and right child, where a None entry means no child. Building stops when the list runs out, which is why trailing None entries can be listed or omitted with the same result. Every child is numbered after its parent, so scanning the numbers from last to first handles both children of a node before the node itself, with no recursion (a 1000-node path would otherwise need 1000 nested calls).

During the scan each node gets one of three states: 0 if it is not monitored yet, 1 if a child's camera monitors it and it holds no camera, and 2 if it holds a camera. A missing child counts as state 1, since it needs no coverage and gives none. If either child is in state 0, the node takes a camera, because only its parent can still reach that child. Otherwise, if either child holds a camera, the node is monitored and takes state 1. Otherwise it takes state 0 and leaves its coverage to its parent. When the scan ends, a root still in state 0 has no parent, so it gets its own camera.

This greedy choice is optimal. A node left in state 0 has both children already monitored, so a camera on it would add only itself and its parent, while a camera on its parent covers those two plus the parent's other child and the parent's own parent. Moving a camera up in this way never loses coverage, so an exchange argument turns any optimal placement into the one the scan builds without adding cameras. A node forced into state 2 has a child that no other node can still reach, so that camera is required.

A single node returns 1, a root with two leaf children returns 1, and a path of k nodes returns ceil(k / 3).

Time complexity:
O(n)
Space complexity:
O(n)