DFS algorithm gfs question

Note submitted and i don’t know where i make error
https://practice.geeksforgeeks.org/problems/depth-first-traversal-for-a-graph/1

here my code :-

vector dfs(vector g[], int N)
{

// Your code here

// helper(g,0);

bool *isVisited = new bool[N];
for(int i = 0; i < N; i++){
    isVisited[i] = false;
}

stack<int> S;
vector<int> V;
S.push(0);
isVisited[0] = true;


while(!S.empty()){
    int top = S.top();
    S.pop();
    V.push_back(top);
    
    //vector<int>::iterator it;
    for(auto it = g[top].rbegin(); it != g[top].rend(); it++){
        if(!isVisited[*it]){
            S.push(*it);
            isVisited[*it] = true;
        }
    }
}
return V;    

}

@khemchandrs share your code using cb ide and why using stack and all , just perform a simple dfs algo as taught

use only itetative way not recursive

@khemchandrs in the question its written do recursive algo . so output is according to that algo starting from 0 ( it dont support multiple answers) here correct one


Given a connected undirected graph. Perform a Depth First Traversal of the graph.
Note: Use recursive approach. (this is written)
dont forget to hit like and mark resolved if cleared :smiley:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.