Easy · Graphs
Graph Traversal
Given node 0 of an undirected graph built from adjacency lists adj, return the labels of all nodes reachable from it in visit order: graphTraversalDFS in recursive depth-first order and graphTraversalBFS in breadth-first order, each taking neighbors in list order.
Examples
Example 1
{
"adj": [
[1, 2],
[0, 3, 4],
[0, 4],
[1, 5],
[1, 2, 5],
[3, 4]
],
"pos": [
[176, 44],
[262, 94],
[262, 193],
[176, 242],
[90, 193],
[90, 93]
]
}Output: BFS [0, 1, 2, 3, 4, 5] · DFS [0, 1, 3, 5, 4, 2]
Example 2
{
"adj": [
[1, 2],
[0, 3],
[0, 3, 4],
[1, 2],
[2]
],
"pos": [
[176, 44],
[270, 112],
[234, 223],
[118, 223],
[82, 112]
]
}Output: BFS [0, 1, 2, 3, 4] · DFS [0, 1, 3, 2, 4]
Rebuild it in the studio
Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.