Binary Tree Cameras
You are given the root of a binary tree. We install cameras on the tree nodes where each camera at a node can monitor its parent, itself, and its immediate children. Return the minimum number of cameras needed to monitor all nodes of the tree.

30:00

Binary Tree Cameras
hard
Topics
Companies

You are given the root of a binary tree. We install cameras on the tree nodes where each camera at a node can monitor its parent, itself, and its immediate children. Return the minimum number of cameras needed to monitor all nodes of the tree.

Example 1:
Input: {"root":[0,0,null,0,0]}
Output: 1
Constraints:
  • The number of nodes is in the range [1,1000][1, 1000].

  • Node.val==0\text{Node.val} == 0

Input
arr ={"root":[0,0,null,0,0]}

4 nodes. A camera watches itself, its parent and its children. Work bottom up so no camera is wasted.

State
nodes
[[object Object], [object Object], [object Object], [object Object]]
width
4
status
[-1, -1, -1, -1]
cameras
0
at
-1
Variables
nodes=4
Variables
VariableValue
nodes4
DepthFunction Call
Stack empty
0/5