Use a worklist-driven algorithm instead of a recursive one.