Find the Connected Component in the Undirected Graph

    Example

    Given graph:

    Return . Since there are two connected component which is
    {A,B,D}, {C,E}

    Java

    注意题目的输出要求,需要为 Integer 和有序。添加 node 至 result 和 visited 时放一起,且只在 dfs 入口,避免漏解和重解。

    复杂度分析

    遍历所有节点和边一次,时间复杂度 O(V+E), 记录节点是否被访问,空间复杂度 O(V).

    源码分析

    同题解一。