As we know, DFS(Depth First Search) is the pre-order traverse of a binary tree. Here is the typical implementaion of DFS,
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?Code:void treeDFS(mynode *root) { if(root == 0) return; printf("[%d] ", root->value); root->visited = 1; if (root->left) { if(root->left->visited==0) { treeDFS(root->left); } } if (root->right) { if(root->right->visited==0) { treeDFS(root->right); } } }




Reply With Quote