BackmediumGraph Basics graph-basics medium

Graph Depth First Traversal Order Solution

Problem Statement

Given a graph represented as an adjacency list, implement a function to perform a depth-first search and return the order in which the nodes are visited. The graph is represented as a list of lists, where each inner list contains the neighbors of a node.

Example 1
Input
[[1, 2], [0, 3], [0, 3], [1, 2]]
Output
[0, 1, 2, 3]

Explanation: The depth-first traversal order of the graph is 0, 1, 2, 3 because we visit node 0 first, then its neighbors 1 and 2, and then their neighbors.

Constraints

  • 1 <= number of nodes <= 100
  • 0 <= number of edges <= 200
  • The graph is connected and undirected
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free