hi i have this code to search a n-ary tree but it dosent works correctly and i dont know whats wrong with that
when searching n4 and n5 it return n3
whats wrong?

public FamilyNode findNodeByName(FamilyNode nodeName ){
if(this.name.equals(nodeName.name)){
// We found a node named nodeName, return it
return this;
}
// That's not me that you are looking for, let's see my kids
for(FamilyNode child : this.children){
if(child.findNodeByName(nodeName) != null)
return child;
// We found what we are looking, just return from here
// return child;
}
// Finished looping over all nodes and did not find any, return null
return null;
}
The reason is because you give back the node in which a node was found. Once a node is found, this node needs to be returned, and the parent nodes in case a
FamilyNodewas found needs to return the foundFamilyNode. Check what is done with thefoundvariable.You need to do something like this:
The entire method will look like: