Binary Tree Cameras
A hard Binary Trees interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Binary Trees
- Sheets
- 1
- Core for
- 4 roles
The problem
You may put a camera on any node of a binary tree. A camera watches its own node, its parent and its direct children. Return the fewest cameras that leave no node unwatched.
Example 1
- Input
- [0,0,null,0,0,null,null,null,null,0,0]
- Output
- 1
- Why
- One camera at the left child of root monitors all nodes: root, itself, and its two children.
Example 2
- Input
- [0,null,0,null,0,0,null,null,0]
- Output
- 2
- Why
- Two cameras needed: one at the second level, one at the fourth level.
Example 3
- Input
- [0]
- Output
- 1
- Why
- A single node requires one camera to monitor itself.
Constraints
- The number of nodes is in the range [1, 1000].
- Node.val is 0.
How to think about it
Updated 2026-09-29Placing a camera on a leaf is the worst possible choice because its coverage can only reach its parent. Cameras should be placed on parents of uncovered nodes. Work bottom-up from the leaves using three states: needs camera (0), has camera (1), and covered (2).
Approaches, worst first
Tree dynamic programming
time O(n) · space O(h)
Define states for each node: `(dp0, dp1, dp2)` where dp0 is min cameras with node unmonitored, dp1 is monitored with camera at node, dp2 is monitored without camera. Compute transition equations combining child states.
Greedy bottom-up tri-state DFSWrite this one
time O(n) · space O(h)
Return 2 for null. For any node, recurse into children. If either child returns 0 (needs coverage), place a camera here (increment count, return 1). If either child returns 1 (has camera), this node is covered (return 2). Otherwise return 0. If the root evaluates to 0 at the end, add one last camera.
Where people lose marks · 3
- Forgetting to check the root after the postorder traversal finishes: if the root returns state 0 (uncovered), an additional camera must be placed at the root.
- Treating null nodes as uncovered (state 0) forces unnecessary cameras onto leaf nodes; null nodes must be treated as already covered (state 2).
- Top-down greedy choices fail because placing cameras at the root cannot anticipate leaf parity deep in subtrees.
The theory behind it
Binary Trees — the ground this problem stands on. All Binary Trees problems
What Binary Trees is
A binary tree is a branching data structure that starts at a single top node called the root, like an upside-down family tree. Every node holds a piece of data and can branch out to at most two children below it, known as the left child and the right child. Because there is no ordering rule about which values go left or right, finding a specific item can require checking every single node in the entire tree.
When to reach for it
Reach for binary trees when problems present hierarchical data with left and right child pointers. Questions asking for tree height, maximum depth, path sums from root to leaf, diameter, lowest common ancestor, or checking whether two trees are mirror reflections of each other all signal binary tree traversals. Any problem asking to inspect or reconstruct a tree layer by layer or path by path belongs here.
How the pattern works
Think recursively by focusing on what a single node must do. If the current node is null, return the base answer immediately. Otherwise, ask the left child for its result, ask the right child for its result, and combine both answers with the current node value before returning up to the parent. For horizontal scans, use a queue to read nodes layer by layer, measuring the queue length at the start of each layer to group nodes by depth.
More Binary Trees problems
Target Roles
Core requirement for 4 roles:
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .