985. Detect Cycle in Directed Graph (DFS)

EasyGraphsDFSGraphCycle

Given a directed graph with n nodes and a list of directed edges [u, v], determine whether it contains a directed cycle. Return true or false. The input is JSON {n, edges}.

Input: JSON {n, edges}.

Output: Boolean — true or false.

Examples

Example 1
Input: {"n":3,"edges":[[0,1],[1,2],[2,0]]}
Output: true
Explanation: A directed cycle exists.
Example 2
Input: {"n":3,"edges":[[0,1],[1,2]]}
Output: false
Explanation: A DAG has no cycle.
Example 3
Input: {"n":1,"edges":[]}
Output: false
Explanation: Single node.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →