Description

Place cameras on tree nodes to monitor all nodes. A camera at node monitors parent, itself, and children. Minimize cameras.

Examples

Input:root = [0,0,null,0,0]
Output:1
Explanation:

One camera at second node monitors all.

Input:root = [0]
Output:1
Explanation:

Single node tree requires one camera. The camera at the root monitors itself, covering all nodes in the tree.

Input:root = [0,0,0,null,null,0,0]
Output:2
Explanation:

Complete binary tree with 5 nodes. One camera at the root monitors the root and both children. One camera at the right child monitors itself and its two children. This placement ensures all nodes are monitored with minimum cameras.

Constraints

  • 1 ≤ number of nodes ≤ 1000

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!