class Solution { public: bool dfs(int node,vector<int> adj[],vector<int> &vis){ vis[node]=1; bool ans=true; for(auto i : adj[node]){ if(vis[i]) return false; ans &=dfs(i,adj,vis); } vis[node]=0; return ans; } vector<int> eventualSafeNodes(int V, vector<int> adj[]) { // code here vector<int> vis(V,0),ans; for(int i=0;i<V;i++){ if(dfs(i,adj,vis)) ans.push_back(i); } return ans; } };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter