My take on depth-first search algorithm