Is Graph Bipartite? LeetCode solution in C++
1class Solution {2public:3bool isBipartite(int node, vector<int> &color, vector<vector<int>> &graph) {4if (color[node] == -1) color[node] = 1;5for (auto it : graph[node]) {6if (color[it] == -1) {7color[it] = !color[node];8if (!isBipartite(it, color, graph)) return false;9} else if (color[it] == color[node])10return false;11}12return true;13}14bool isBipartite(vector<vector<int>> &graph) {15int n = graph.size();16vector<int> color(n, -1);17for (int i = 0; i < n; i++)18if (color[i] == -1)19if (!isBipartite(i, color, graph)) return false;20return true;21}22};