Is Graph Bipartite? LeetCode solution in JavaScript

1
var isBipartite = function (graph) {
2
const colors = new Map();
3
const stack = [];
4

5
for (let i = 0; i < graph.length; i++) {
6
if (colors.has(i)) continue;
7

8
colors.set(i, true);
9
stack.push(i);
10

11
while (stack.length > 0) {
12
let current = stack.pop();
13

14
for (let neighbour of graph[current]) {
15
if (!colors.has(neighbour)) {
16
colors.set(neighbour, !colors.get(current));
17
stack.push(neighbour);
18
continue;
19
}
20

21
if (colors.get(neighbour) === colors.get(current)) return false;
22
}
23
}
24
}
25

26
return true;
27
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0