Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Link to the question : https://practice.geeksforgeeks.org/problems/depth-first-traversal-for-a-graph/1
- void dfs(vector<int> adj[],vector<bool> &visit,vector<int> &ans, int curr){
- visit[curr] = true;
- ans.push_back(curr);
- for(int i = 0; i<adj[curr].size() ; i++){
- int ncurr = adj[curr][i];
- if(visit[ncurr]==false){
- dfs(adj,visit,ans,ncurr);
- }
- }
- return;
- }
- class Solution
- {
- public:
- //Function to return a list containing the DFS traversal of the graph.
- vector<int>dfsOfGraph(int V, vector<int> adj[])
- {
- // Code here
- vector<bool> visit (V,false);
- vector<int> ans;
- dfs(adj,visit,ans,0);
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement