785. Is Graph Bipartite?
```java
class Solution {
public boolean isBipartite(int[][] graph) {
int n = graph.length;
int[] color = new int[n]; // 0 , 1, -1
for (int i = 0; i < n; i++) {
if (color[i] == 0) {
if (!dfs(graph, color, i, 1)) {
return false;
}
}
}
return true;
}
private boolean dfs(int[][] graph, int[] color, int i, int wantedColor) {
if (color[i] != 0) {
return color[i] == wantedColor;
}
color[i] = wantedColor;
for (int next : graph[i]) {
if (!dfs(graph, color, next, wantedColor*(-1))) {
return false;
}
}
return true;
}
}
```
Last updated