Now I understand your requirements. I can't say I have a solution just yet. This isn't a specific
JAVA problem, but more like a Data Structures and Algorithm problem. Unfortunately, we don't have a DS&A forum.
Have you tried coming up with a solution without using recursion? This might be easier. If you do get it working without recursion, it might be easy to figure out how to apply the recursive routine from a working solution.
My $0.02,
-Peter