← All problems

Find if Path Exists in Graph

Easy graphsdfsbfs LeetCode ↗
There is a bi-directional graph with n vertices labeled 0 to n-1 and a list of undirected edges. Given a source vertex and a destination vertex, determine whether a valid path exists between them. Return true if they are connected, false otherwise.

Hints (0/3 revealed)

Log in to reveal hints progressively.

Complexity

Time O(n + e), space O(n + e) for the adjacency list and visited set.

Common pitfalls

  • Treating edges as directed when they are bi-directional.
  • Missing the trivial case where source equals destination (path of length 0 exists).
  • Rebuilding the adjacency list per query instead of once.

Your solution

Log in to save your code and track progress.