In the way of graph traversal, DFS and BFS are two main ways of traversal. DFS is primarily implemented using recursive thinking, and BFS mainly uses queues to hold the nodes below. One advantage of BFS is not the non-recursive form, so the likelihood of stack overflow is very small, contrary to DFS in this respect is relatively large. Therefore, how to change DFS to non-recursive form is significant.
How do I turn Dfs into a non-recursive form?
In fact, with a large hat in front of it, it is not very difficult for DFS to implement a non-recursive implementation.
The node we are going to access is therefore put into a stack, and the node is marked for access, and if the node has a child, it is considered that the child has been visited in the case of a sub-RU stack.
After the next step from the stack to pop a node, first of all to determine whether the node has been visited, if accessed, then skip, continue to judge.
[One algorithm] graph DFS non-recursive implementation