Your frontend talks to a tree of microservices. A health monitor installed on a service watches that service, its parent, and its children. Every service must be watched by at least one monitor.
Write fewestMonitors(root) that returns the smallest number of monitors needed to watch every node of the tree. The tree has at least one node. Node values don't matter.
Input: root = [0,0,null,0,0] Output: 1
One monitor on the middle node watches its parent and both of its children.
Input: root = [0,0,null,0,null,0,null,null,0] Output: 2
This chain of five needs two monitors, for example on the 2nd and 4th nodes.
1 <= number of services <= 1000Trees are written level by level, like [1, 2, 3, null, 4], where null is a missing child. Your function works with TreeNode objects, each with val, left, and right.
Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #351 Fewest Health Monitors. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.