I have a tree class that looks like:
Class Tree {
Node root;
Node curNode;
public List find(String value) {
if (curNode == null)
The following code traces the path, adding nodes to the list and removing them if they are not in the path
boolean getPath(Node root,String targetValue,List path)
{
// base case root is null so path not available
if(root==null)
return false;
//add the data to the path
path.add(root.getData());
//if the root has data return true,path already saved
if(root.getData().equals(targetValue))
return true;
//find the value in all the children
for(Node child: children){
if(getPath(child,targetValue,path))
return true;
}
//if this node does not exist in path remove it
path.remove(path.size()-1);
return false;
}