Binary Tree Cameras is a fascinating optimization problem that challenges you to find the minimum number of security cameras needed to monitor an entire binary tree.

Imagine you're designing a security system for a hierarchical network where each node represents a location. You need to strategically place cameras at certain nodes to monitor the entire network. Each camera has a monitoring range that covers:

• The node where it's installed
• Its parent node
• All its immediate children

Your goal is to determine the minimum number of cameras required to ensure every single node in the binary tree is monitored by at least one camera. This is a classic example of a greedy algorithm combined with dynamic programming on trees.

Input: The root of a binary tree
Output: Minimum number of cameras needed to monitor all nodes

Input & Output

example_1.py — Simple Tree
$ Input: root = [0,0,null,0,0]
Output: 1
💡 Note: One camera at the root's left child can monitor the root, its left child, and the left child's children. This covers all 4 nodes with just 1 camera.
example_2.py — Linear Tree
$ Input: root = [0,0,null,0,null,0,null,null,0]
Output: 2
💡 Note: This is essentially a linear chain. We need cameras at alternating positions to cover the entire chain efficiently. Placing cameras at positions that can cover 3 nodes each gives us the minimum.
example_3.py — Single Node
$ Input: root = [0]
Output: 1
💡 Note: A single node tree requires exactly one camera to monitor itself, as there are no parent or children to help with coverage.

Visualization

Tap to expand
Corporate Security NetworkCEOVP1VP2MGR1MGR2Optimization Strategy:🏢 Each camera monitors 3 levels: • Parent office (supervisor) • Current office (self) • Direct subordinate offices💡 Key Insight:Place cameras at parent level tomaximize coverage efficiency🎯 Result:1 camera covers entire networkof 5 offices - optimal solution!
Understanding the Visualization
1
Bottom-Up Analysis
Start from the leaf offices (bottom of hierarchy) and work upward
2
Greedy Placement
Place cameras only when absolutely necessary - when subordinates need monitoring
3
State Propagation
Each camera placement affects the monitoring status of neighboring offices
4
Optimal Coverage
Achieve complete network security with minimum hardware investment
Key Takeaway
🎯 Key Insight: The greedy bottom-up approach ensures optimal camera placement by prioritizing parent nodes over leaf nodes, maximizing the coverage area of each camera while minimizing the total number needed.

Time & Space Complexity

Time Complexity
⏱️
O(n)

Single DFS traversal visits each node exactly once

n
2n
Linear Growth
Space Complexity
O(h)

Recursion stack depth equals tree height h, O(log n) for balanced trees

n
2n
Linear Space

Constraints

  • The number of nodes in the tree is in the range [1, 1000]
  • Node values are always 0 (values don't affect the camera placement)
  • Each camera monitors exactly its parent, itself, and immediate children
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
43.5K Views
Medium Frequency
~25 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen