Find the minimum moves to visit all critical nodes in a tree and return to the root.
Unlock detailed company stats for this questionUpgrade
A large distributed system is modeled as a general tree of N servers (nodes 1..N), undirected and acyclic. Some servers contain critical logs that must be collected; these are the set S. You begin at server 1.
Goal: start at node 1, visit every node in S at least once, and return to node 1. In one move you travel to an adjacent node. Determine the minimum number of moves.
Input: integer N; N-1 edges (u v); the set S. Output: a single integer, the minimum number of moves.
Example: N = 8, S = {5, 6}, edges 1-2, 2-5, 2-3, 2-6, 1-4, 4-7, 7-8 gives 6 (journey 1 -> 2 -> 5 -> 2 -> 6 -> 2 -> 1).
Expect follow-ups like
- You are allowed to start from 2 places, {1, N}: in one move you can select either of the start points and move to an adjacent node.
Community answers
No answers contributed by the community yet.


