不知写个non-recursive的DFS会不会好点,减少system stack call. 要用一个stack,但因为我想打印出路径,所以觉得用一个deque来保存各个点比较好点。 void FindAllPath(G, source, dest) deque d; d.push_back(source); //这里还要一个数据结构visit表示点是否访问过,跟DFS那里一样。 while(!d.empty()) u = d.back(); for(each v in u's neighbor) if( v == dest) print path (that is from d's front to end) else if v has been visited continue; else d.push_back(v); end if u = d.back(); end for u = d.back(); mark u visited; d.pop_back(); end while