A Lucene search may do what you're after - perhaps something like:
TYPE:cm\:folder
A couple of notes:
This won't find any spaces that have custom types (eg. forums) - you could extend the query to support this quite easily, provided you know all of the different folder types ahead of time (or can calculate them in your code).
This won't find any folders within a Web Project.
This will find all spaces that the user has read access to - if you're trying to find spaces per role (eg. "get all spaces where the user is a Contributor") then this approach won't be appropriate.