Shortest walk visiting all nodes

Write codeHard40 min

Your task

You are given an undirected, connected graph of n nodes (0..n-1) as an adjacency list. Return the length of the shortest walk that visits every node at least once. You may start and stop at any node, and you may revisit nodes and re-traverse edges (each edge traversed counts as 1 step). For n==1 the answer is 0.

Examples

shortest_path_all_nodes(graph: list[list[int]]) -> int
Example 1
Inputshortest_path_all_nodes([[1, 2, 3], [0], [0], [0]])
Output4