All of the test inputs are edge cases and you don’t show a clear example that maps the adjacency matrix to the nodes.
Even your first example question is an edge case and it has a ridiculous explanation.
“Both nodes minimize the malware spread. So we will pick the node with the smallest index, which is 1.”
No both nodes do not minimize the malware spread, you have 2 nodes with malware in the same tree, so that network is doomed and cant be saved, it is an edge case so it defaults to the minimum. This is a stupid requirement, it should return -1 since no network can be saved.
Course: Grokking Coding Interview Patterns in C++ - Learn Interactively
Lesson: Minimize Malware Spread - Grokking Coding Interview Patterns in C++