If you follow the directory templates, you'll want an accumulator for all the directory names you've ever seen while looking through the tree.
As another approach that has some strong similarities, flatten the tree first into a big list, and then look for duplicates in the result. That way, you get to ignore the tree structure.
Ignoring the data's given structure is normally considered a bad thing. In this case, it's can be viewed as reasonable, since the result is explicitly independent of the tree structure.