DFS Traversal Problem
DFS Traversal Problem — ExecCode Easy DSA Practice
Solve the DFS Traversal problem on ExecCode. Free online easy DSA practice in Arrays - Basics. Write and run code in Java, C++, Python — no signup required to run.
Problem description
Given an undirected graph as an adjacency list and a start node, return the DFS visit order using recursion (preorder: record a node when first entered).
Examples
Input n = 7, adj = [[1, 2], [0, 3, 4], [0, 4, 5], [1, 6], [1, 2, 6], [2], [3, 4]], start = 0; Output [0, 1, 3, 6, 4, 2, 5]. Input n = 4, adj = [[1, 2], [0], [0, 3], [2]], start = 0; Output [0, 1, 2, 3]
Constraints
1 ≤ n ≤ 10⁴ 0 ≤ start < n Graph may contain cycles — use a visited set Adjacency lists are iterated in given order
Practice DFS Traversal free on ExecCode. Browse DSA problems, topic map, and placement guides.