DFS of Graph

Problem Link: https://practice.geeksforgeeks.org/problems/depth-first-traversal-for-a-graph/1

class Solution:
    
    #Function to return a list containing the DFS traversal of the graph.
    def dfsOfGraph(self, V, adj):
        # code here
        
        def dfs(node,adj):
            
            vis[node]=1
            ans.append(node)
            
            for i in adj[node]:
                if(vis[i]==0):
                    dfs(i,adj)
            
        ans=[]
        vis=[0]*(V)
        dfs(0,adj)
        return ans

Last updated