### chinesecoder's blog

By chinesecoder, history, 5 months ago, ,

suppose we are doing dfs on tree .

Your code here...

dfs(int current ,  int parent)
{
for(auto child : v[current]
{
if(child == parent) continue ;
dfs(child , current);
code...
what happens when we write something here.

}
code ....
And what happens when we write something here ...

}


what are the difference between two above .

Please tell , i have difficulty in distinguishing between two

• -4

 » 5 months ago, # |   0 In the first, you do something after you visit each child (e.g. count number of subtrees).In the second, you do something just before you exit the node (e.g. mark this node as visited).
•  » » 5 months ago, # ^ | ← Rev. 2 →   0 ok .. i understand the first one.. that in case 1 while visiting every child we also do some operations simultaneously. and the operation will only util the last child is visted.. just like a part of subtree.case u eleborate 2nd case. what u mean here by exiting the node. does it mean that when we explored all the child of a current node. then we do some processing in step2 ? please tell
•  » » » 5 months ago, # ^ |   0 Yes. For the second case, the operation is done only after all subtrees have been visited and before the recursion returns to the parent node.
•  » » » » 5 months ago, # ^ |   0 Thanks