The control flow of this function is hard to understand (e.g., breaks inside while loops, etc.).  Is there a simpler way to write it?  I think this function, would be easier to implement in recursive style instead of work-list style.  (Recur on the "next" and "child".)