1072. Check if Graph is Bipartite (BFS)

MediumGraphsGraph ColouringBFSGraph

Given an undirected graph with n nodes and edges, determine whether it is bipartite using a BFS two-coloring. Return true or false. The input is JSON {n, edges}.

Input: JSON {n, edges}.

Output: Boolean — true or false.

Examples

Example 1
Input: {"n":4,"edges":[[0,1],[1,2],[2,3],[3,0]]}
Output: true
Explanation: An even cycle is bipartite.
Example 2
Input: {"n":3,"edges":[[0,1],[1,2],[2,0]]}
Output: false
Explanation: An odd cycle is not.
Example 3
Input: {"n":5,"edges":[[0,1],[2,3]]}
Output: true
Explanation: Disjoint edges are bipartite.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →