I was trying to detect a cycle in a directed graph.
While coming up with the logic to solve it, I figured out that a simple graph traversal eq. dfs is sufficient because while doing dfs we can just have a condition to see if any node is already visited. If so, there must be a cycle.
While, looking up the solution to cross check my logic I came across this solution which says that while doing dfs along with keeping track of the visited nodes you also need to keep track of the nodes in the recursion stack and is a node is already in the recursion stack then there is a cycle- which I do not understand.
Why do we need to keep track of the nodes in the recursion stack when we can simply just check if a node is visited again and conclude there is a cycle?