Note this commented "solution button".  You should distinguish what you hide and the test you show.  Printing the state is rather part of the solution — to help the reader to figure out how it works.  Since the question asks for a  transversal, a `fold` function visiting the nodes in depth-first order may be used to design various examples.