Euler Tour of a tree

Consider the following given tree:

I want to get the following output:
1 2 4 2 5 2 6 2 1 3 7 8 7 9 7 3 1

This is also called the Euler Tour of the tree.

However, if I use DFS I get the follwing output:
1 2 4 5 6 3 7 8 9

Can you tell how to modify the DFS code in such a manner that I get the required output?

pls share ur code… i will help u out…

Sir I could only write the code for Depth First Search of the graph… that is a very basic code.

I want a code that will give me this as the output:
1 2 4 2 5 2 6 2 1 3 7 8 7 9 7 3 1

yrr that is what i am saying… u said u are getting the o/p 1 2 4 5 6 3 7 8 9… share ur code i will make changes…

This is the code sir:

Please tell how to edit it to get required output.

What I have done is simply Depth First Search (DFS)


this seems fine to me

Sir you have output 1 by yourself in the main function

It should get printed by the function

i have just printed the root node explicitly…

Yes sir, but when I will submit it anywhere there would be different root nodes. The code will fail in such a case

Basically what I want is that the root node should also get printed by itself in the function. Also sir, can you please explain the recursion calls of the solution that you sent me? (that is which function called which function etc.) I am not able to get it

actually it is not possible to explain the same over here… try to dry run on paper… maybe u would get the intuition

Sir I didn’t understand how cout << src after the recursive calls is printing the nodes

try to dry run a bit… u will get the logic

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

Doubt is still not clear… I am not getting desired output from your code

pls share the ques link…

try this algorithm

void euler(node* t){

if(t==NULL){ // if t is child of a leaf node

    return ;

}



for(...){ //propogate through all childs of t

    euler(t->child1);

    euler(t->child2);

    euler(t->child3);

    .

    .

    .

    .

    euler(t->lastchild);

}

cout<<t->data<<" ";

return;

}