Hello Friends, its me again....back with another problem.
I am trying to write a method which goes through my tree, finds the element given to the function and gives back the path (nodes) I traversed while I was searching for that element/ object.
My Idea was to work with a few helper-methods to solve the problem.
1. Write a method to check if there is a path from each child of my current node.(There is always only one child-node option that works at each level)
2. If there is a path I want to save the node and check the same thing for the child. I don't care about the nodes that don't have a path.
My questions now:
- Is there a smooth way to store the my paths? Thought about an ArrayList where I add the [i] that leads to the correct path.
- Does my pathExist function work? To be honest I am not even sure if i get the correct result that I want...
Hope this is not to vague and someone can maybe give me a Tip or send me a link to check a concept?
I only find Binary-Tree solutions.
Are you asking how to traverse your tree or only how to record the nodes visited? If you are visiting the nodes recursively, one way to do that is to pass a List<Node> as an argument and add each Node visited to the List after the recursive call. It may be easier to add the nodes to a Stack implementation before the recursive call. Both those suggestions will show the leaf node first and the root last.