Recursion can be difficult to explain, but I'll try my best explaining how it applys in this situation. Since you are able to recursively check weither each node exists, you will want to return the id as the return string. This notifies up the recursive stack that a match has been found. You then append the current node's id to the string and return it up the stack. This in turn notifies up the stack that a match has been found, etc. Here is my solution, which I've added multiple comments to better illustrate the point.
// If found return [string] [of] [ids], else empty string
string look(const Tree * t, const string & id) {
// If current node matchs return id
if(t->id == id) {
return "[" + id + "]";
}
// Loop over each child, recursively calling look
for(int i=0; i<t->numof_children; i++) {
string s = look(t->children[i], id);
// If found add to string and return
if(!s.empty()) {
return "[" + t->id + "] " + s;
// alternatively: return s + " [" + t->id + "]"; will genreate string in reverse
}
}
// Not found, return empty string
return string();
}
Most recursive functions follow a similar pattern. If you still a little lost, I would recommend running it through a debugger so you can see exactly what is going on.