Skip to content

Practice Shortest Path Visiting All Nodes

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#847
LeetCode Problem

Shortest Path Visiting All Nodes

Problem Statement

You have an undirected, connected graph of n nodes labeled from 0 to n - 1. You are given an array graph where graph[i] is a list of all the nodes connected with node i by an edge. Return the length of the shortest path that visits every node. You may start and stop at any node, you may revisit nodes multiple times, and you may reuse edges.

Source: Shortest Path Visiting All Nodes on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
graph = [[1,2,3],[0],[0],[0]]
Output
4

# Example 2

Input
graph = [[1],[0,2,4],[1,3,4],[2],[1,2]]
Output
4

Constraints

n == graph.length
1 <= n <= 12
0 <= graph[i].length < n
graph[i] does not contain i.
If graph[a] contains b, then graph[b] contains a.
The input graph is always connected.

Related LeetCode Problems

Practice more problems similar to Shortest Path Visiting All Nodes.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.