Introduction
Binary Tree Cameras means:
- placing minimum cameras
inside binary tree
to monitor all nodes
Each camera can monitor:
- parent node
- current node
- immediate child nodes
Goal:
- cover entire tree
- using minimum cameras
Example:
0/ \
0 0
/ \
0 0
Minimum Cameras:
2
Explanation:
Strategic camera placement covers maximum nodes efficiently. This problem is one of the most important applications of:
DFS Greedy Traversal Constraints
1 <= Number of Nodes <= 10^5Approach : Greedy DFS Traversal
Explanations:
Explanation:
The idea is:
- process tree bottom-up
- place camera only when needed
- avoid unnecessary cameras
Node States:
0 → Needs camera
1 → Has camera2 → Covered
Steps:
- Traverse left subtree.
- Traverse right subtree.
- Check child states.
- Place camera if needed.
- Mark covered nodes.
Greedy Rule:
Place camera only when child needs coverage. This approach:
- uses DFS recursion
- applies greedy optimization
Dry Run
Visit leaf node.Leaf children:
covered
Leaf state:
needs camera
Parent places camera.
Covered nodes increase.
Practice :