rewrite solution of graph traverse problem