You are given a graph with V vertices and its adjacency list. Starting from vertex 0, perform a Depth First Search (DFS) traversal and return the order in which the vertices are visited.
Input: V = 5, adj = [[1,2],[0,3],[0,4],[1],[2]]
Output: [0,1,3,2,4]
Explanation: The vertices are visited using Depth First Search starting from vertex 0.
Input: V = 3, adj = [[1],[0,2],[1]]
Output: [0,1,2]
Explanation: DFS visits all reachable vertices.
Input: V = 1, adj = [[]]
Output: [0]
Explanation: The graph contains only one vertex.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
5 4 0 1 0 2 1 3 2 4
0 1 3 2 4
6 5 0 1 0 2 1 3 2 4 2 5
0 1 3 2 4 5