582. BFS Level Order Traversal Using Queue

EasyStackQueueBFSGraph

Given an undirected graph with n nodes and edges, and a start node, return the order in which nodes are first visited by a breadth-first search using a queue, exploring each node's neighbors in ascending order. The input is JSON {n, edges, start}.

Input: JSON {n, edges, start}.

Output: Array — the BFS visitation order.

Examples

Example 1
Input: {"n":5,"edges":[[0,1],[0,2],[1,3],[2,4]],"start":0}
Output: [0,1,2,3,4]
Explanation: Level-by-level from node 0.
Example 2
Input: {"n":1,"edges":[],"start":0}
Output: [0]
Explanation: Single node.
Example 3
Input: {"n":3,"edges":[[0,1],[1,2]],"start":1}
Output: [1,0,2]
Explanation: Neighbors of 1 in ascending order.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →