My question is that why'd we need to mark each visited vertex? If I follow the pre-order , I don't need to mark each visited vertex. Any guru here can give me an explaination why?
DFS is a general search algorithm for graphs, not just for trees, and as such, it needs to be informed which nodes have already been visited since it might be possible to reach the same node several times from different paths.
The implementation you suggested only fits binary trees, and won't work for any general graph.
DFS is a general search algorithm for graphs, not just for trees, and as such, it needs to be informed which nodes have already been visited since it might be possible to reach the same node several times from different paths.
The implementation you suggested only fits binary trees, and won't work for any general graph.
Bookmarks