Skip to content

Commit 9a64aa0

Browse files
authored
Create code.cpp
1 parent 61f8f05 commit 9a64aa0

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

bfs_graph/code.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public:
3+
// Function to return Breadth First Traversal of given graph.
4+
vector<int> bfsOfGraph(int V, vector<int> adj[]) {
5+
// Code here
6+
bool vis[V];
7+
for(int i=0;i<V;i++){
8+
vis[i]=false;
9+
}
10+
11+
vis[0]=true ;
12+
13+
queue<int>q;
14+
q.push(0);
15+
vector<int>res;
16+
17+
while (!q.empty()){
18+
int s=q.front();
19+
res.push_back(s);
20+
21+
q.pop();
22+
for(auto i : adj[s]){
23+
if(!vis[i]){
24+
q.push(i);
25+
vis[i]=true;
26+
}
27+
28+
}
29+
}
30+
return res;
31+
32+
}
33+
};

0 commit comments

Comments
 (0)