979. Check if Path Exists Between Two Nodes

EasyGraphsDFSGraph

Given an undirected graph with n nodes and a list of edges, determine whether there is any path from src to dst. Return true or false. The input is JSON {n, edges, src, dst}.

Input: JSON {n, edges, src, dst}.

Output: Boolean — true or false.

Examples

Example 1
Input: {"n":5,"edges":[[0,1],[0,2],[1,3],[2,4]],"src":0,"dst":4}
Output: true
Explanation: 0-2-4 connects them.
Example 2
Input: {"n":5,"edges":[[0,1],[2,3]],"src":0,"dst":3}
Output: false
Explanation: Different components.
Example 3
Input: {"n":1,"edges":[],"src":0,"dst":0}
Output: true
Explanation: Same node.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →