Best O(N) Solution
Approach\nUsing DFS Traversal\n\n# Complexity\n- Time complexity:\nO(N) + O(N + 2E) --> O(N)\n\n- Space complexity:\nO(N) + O(N) --> O(N)\n\n# Code\n\nclass Sol
# Approach\nUsing DFS Traversal\n\n# Complexity\n- Time complexity:\n$$O(N) + O(N + 2E)$$ --> $$O(N)$$\n\n- Space complexity:\n$$O(N) + O(N)$$ --> $$O(N)$$\n\n# Code\n```\nclass Solution {\nprivate:\n    void dfs(int node, vector<vector<int>>& isConnected, vector <bool>& vis) {\n        vis[node] = true;\n        for (int i = 0; i < vis.size(); i++) {\n            if (node != i && !vis[i] && isConnected[node][i])\n                dfs(i, isConnected, vis);\n        }\n    }    \npublic:\n    int findCircleNum(vector<vector<int>>& isConnected) {\n        int n = isConnected.size();\n        vector <bool> vis(n, false);\n        int cnt = 0;\n        for (int i = 0; i < vis.size(); i++) {\n            if (!vis[i]) {\n                cnt++;\n                dfs(i, isConnected, vis);\n            }\n        }\n        return cnt;\n    }\n};\n```
['C++']