in reply to A maybe(?) interesting comp-sci type problem

If I understand it correctly, you don't want to flatten the tree, but build possible combinations from it, so
[A, [B, C]]
becomes
[A, B], [A, C]
In "Higher Order Perl" are some algorithms that do similar things with iterators.

If you don't have a copy of that book available (which would be a shame, but happens nonetheless ;-)) you should first write a sub that purges any subtrees deeper than 5 (in your example), and then a recursive one that does the expansion.