425. Find eventual safe states
Given a directed graph with V vertices labeled from 0 to V-1. The graph is represented using an adjacency list where adj[i] lists all nodes adjacent to node i, meaning there is an edge from node i to each node in adj[i]. A node is a terminal node if there are no outgoing edges. A node is a safe node if every possible path starting from that node leads to a terminal node. Return an array containing all the safe nodes of the graph in ascending order.
Example 1:
Input:Â V = 7, adj= [[1,2], [2,3], [5], [0], [5], [], []]

Output: [2, 4, 5, 6]
Explanation:Â
From node 0: two paths are there 0->2->5 and 0->1->3->0.Â
The second path does not end at a terminal node. So it is notÂ
a safe node.
From node 1: two paths exist: 1->3->0->1 and 1->2->5.
But the first one does not end at a terminal node. So it is not a safe node.
From node 2: only one path: 2->5 and 5 is a terminal node.
So it is a safe node.
From node 3: two paths: 3->0->1->3 and 3->0->2->5Â
but the first path does not end at a terminal node.Â
So it is not a safe node.
From node 4: Only one path: 4->5 and 5 is a terminal node.Â
So it is also a safe node.
From node 5: It is a terminal node.Â
So it is a safe node as well.
From node 6: It is a terminal node.Â
So it is a safe node as well.
Example 2:
Input:Â V = 4, adj= [[1], [2], [0,3], []]

Output: [3]
Explanation: Node 3 itself is a terminal node and it is a safe node as well. But all the paths from other nodes do not lead to a terminal node.So they are excluded from the answer.
Now Your Turn!
Pick the correct output for the given inputInput:Â V = 4, adj= [[1], [2], [0], []]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- Â Â V == adj.length
- Â Â 1 <= V <= 104
- Â Â 0 <= adj[i].length <= n
- Â Â 0 <= adj[i][j] <= n - 1
- Â Â adj[i] is sorted in a strictly increasing order.
- Â Â The graph may contain self-loops.
- Â Â The number of edges in the graph will be in the range [1, 4 * 104].