Is Graph Bipartite? LeetCode solution in JavaScript
1var isBipartite = function (graph) {2const colors = new Map();3const stack = [];45for (let i = 0; i < graph.length; i++) {6if (colors.has(i)) continue;78colors.set(i, true);9stack.push(i);1011while (stack.length > 0) {12let current = stack.pop();1314for (let neighbour of graph[current]) {15if (!colors.has(neighbour)) {16colors.set(neighbour, !colors.get(current));17stack.push(neighbour);18continue;19}2021if (colors.get(neighbour) === colors.get(current)) return false;22}23}24}2526return true;27};