You are given an undirected, unrooted tree with n nodes indexed from edges of length edges[i] = [a_i, b_i] indicates an undirected edge between nodes a_i and b_i. You are also given a binary array coins of size n, where coins[i] is either i contains a coin.
You may begin at any node in the tree and repeatedly perform the following operations in any order:
Collect all coins located at a distance of at most
Move to any directly adjacent node in the tree.
Return the minimum number of edges you must traverse to collect every coin and return to your starting node.
Note: If the same edge is traversed multiple times, each traversal counts separately toward the total.
Constraints:
n == coins.length
n
coins[i]
edges.length == n - 1
edges[i].length == 2
a_i, b_i n
a_i != b_i
edges represents a valid tree.
You are given an undirected, unrooted tree with n nodes indexed from edges of length edges[i] = [a_i, b_i] indicates an undirected edge between nodes a_i and b_i. You are also given a binary array coins of size n, where coins[i] is either i contains a coin.
You may begin at any node in the tree and repeatedly perform the following operations in any order:
Collect all coins located at a distance of at most
Move to any directly adjacent node in the tree.
Return the minimum number of edges you must traverse to collect every coin and return to your starting node.
Note: If the same edge is traversed multiple times, each traversal counts separately toward the total.
Constraints:
n == coins.length
n
coins[i]
edges.length == n - 1
edges[i].length == 2
a_i, b_i n
a_i != b_i
edges represents a valid tree.