forked from javadev/LeetCode-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
30 lines (27 loc) · 888 Bytes
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package g0701_0800.s0785_is_graph_bipartite;
// #Medium #Depth_First_Search #Breadth_First_Search #Graph #Union_Find
// #Graph_Theory_I_Day_14_Graph_Theory #2022_03_26_Time_0_ms_(100.00%)_Space_54.3_MB_(18.06%)
public class Solution {
public boolean isBipartite(int[][] graph) {
int n = graph.length;
int[] color = new int[n];
for (int i = 0; i < n; i++) {
if (color[i] == 0 && !helper(graph, i, -1, color)) {
return false;
}
}
return true;
}
private boolean helper(int[][] graph, int curr, int c, int[] color) {
if (color[curr] == c) {
return true;
}
color[curr] = c;
for (int x : graph[curr]) {
if (color[x] == c || !helper(graph, x, c * -1, color)) {
return false;
}
}
return true;
}
}