代碼模板:
List<Integer> resultList = new ArrayList<>();List<Integer> hierholzer() {dfs(0);resultList.add(0);// 數組反轉Collections.reverse(resultList);return resultList;
}void dfs(int start) {for(int end : G[start]) {if(!vis[start][end]) {vis[start][end] = true;dfs(end);resultList.add(end);}}
}
算法圖示:
最終得到歐拉回路:0 ->1 -> 2 -> 4?-> 5?-> 2?-> 3?-> 0