#968

Binary Tree Cameras

candidate master · 1480 · lc hard +32 · verified · 47.7% accepted · 5,643 likes · top 33%

Description

Install cameras on nodes of the binary tree root. Each camera covers itself, its parent, and its immediate children. Return the minimum number of cameras needed to monitor every node.

Example 1:

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

Example 2:

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.

Code

1
2
3
4
5
6
7
8
9