图的遍历

 

function dfs(graph, visited, res, vet) {
    res.push(vet);
    visited.add(vet);
    const adjVets = graph.adjList.get(vet);
    for (const adjVet of adjVets) {
        if (visited.has(adjVet)) {
            continue;
        }
        dfs(graph, visited, res, adjVet);
    }
}

function graphDFS(graph, startVet) {
    const res = [];
    const vis = new Set();
    dfs(graph, vis, res, startVet);
    return res;
}

 

posted @ 2024-04-09 23:27  樊顺  阅读(1)  评论(0编辑  收藏  举报