 All Problems
Binary Tree Cameras
hard
tree
depth-first search
greedy
dynamic programming
binary tree
google
amazon

You are given a binary tree where each node can have a camera installed. Cameras on a node monitor that node, its parent, and its immediate children. Return the minimum number of cameras needed to monitor all nodes of the tree.

Example 1:

Input:  0 0 null 0 0
Output: 1

Example 2:

Input:  0 0 null 0 null 0 null null 0
Output: 2

Constraints:

  • The number of nodes is in the range [1, 1000]
  • Node.val is always 0 (value doesn't matter, only structure)

Input format: BFS level-order, space-separated, null for missing nodes.

Output format: Minimum number of cameras.

Run to check your code against the sample cases, or submit to run every case