与BFS 不同,更早访问的结点可能不是更靠近根结点的结点。因此,你在 DFS 中找到的第一条路径可能不是最短路径。 模板- 递归 /* * Return true if there is a path from cur to target. */booleanDFS(Nodecur,Nodetarget,Set<Node>visited){returntrueifcuristarget;for(next:each neighbor of cur){if(...