You will need to use this function recursively and mantain the visiting of the nodes. you will need to keep track of all the nodes which have been traversed and on that criteria you will call this dunction. At the moment you are just calling this function from any location and it gets stuck in loop. So in order to break that loop you will need a criteria to break the loop. And that criteria can be fulfilled by using another linked list or a variable which will hold the number of nodes traversed and then compare that variable with the actual number of nodes of the tree.
Hopefully this answers your question.