Skip to content

Commit ba27de7

Browse files
authored
Create main.cpp
1 parent 3424d6b commit ba27de7

File tree

1 file changed

+20
-0
lines changed
  • 23 - Graph Data Structure Problems/06 - DFS of Graph

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
void dfsUtil(int node, vector<vector<int>>& adj, unordered_map<int, bool>& visited, vector<int>& result){
4+
visited[node] = true;
5+
result.push_back(node);
6+
7+
for(int neighbor : adj[node]){
8+
if(!visited[neighbor]) dfsUtil(neighbor, adj, visited, result);
9+
}
10+
}
11+
12+
vector<int> dfsOfGraph(vector<vector<int>>& adj) {
13+
int V = adj.size();
14+
unordered_map<int, bool> visited;
15+
vector<int> result;
16+
17+
dfsUtil(0, adj, visited, result);
18+
return result;
19+
}
20+
};

0 commit comments

Comments
 (0)