You can tell which visit action to perform at a node by taking note of where you are coming from.