Fewest Health Monitors to Cover Every Node in a Network Tree
Last updated: November 29, 2026
Quick Overview
A network topology is a binary tree. A monitor placed on a node covers that node, its parent and its children. Return the minimum number of monitors needed so every node is covered.
Meta
November 29, 20260
0
0 solved
A network topology is a binary tree. A monitor placed on a node covers that node, its parent and its children. Return the minimum number of monitors needed so every node is covered.
A hard Meta onsite tree problem where the elegant solution is a greedy post-order with three states. The interviewer wants you to justify why placing monitors at parents of uncovered leaves is optimal, not just state the rule.
What the Interviewer Expects
- Define three states: needs coverage, covered without monitor, has monitor
- Process children before parents
- Place a monitor when any child needs coverage
- Handle the root needing coverage at the end
- Argue the greedy exchange: a leaf's parent always dominates the leaf
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Why is it never worse to put the monitor on the leaf's parent than on the leaf?
- How would you solve it with a DP of three values per node instead?
- What if monitors have different costs per node?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input is a binary tree root with at least one node. A monitor on a node covers it, its parent, and its immediate children. Return the minimum number of monitors to cover all nodes....
High-Level Design
Assign each node a state after its children are done: 0 means 'not covered yet', 1 means 'has a monitor', 2 means 'covered by a child, no monitor'. Missing children count as 2 so leaves are not forced...