Algorithms and Data Structures · Junior · Question 53 of 75
What is the difference between depth-first (DFS) and breadth-first (BFS) traversal?
- ✓Correct answer: DFS follows one branch to the end first; BFS goes level by level
- 2DFS visits nodes in sorted order, BFS in random order
- 3DFS works only on trees, BFS works only on graphs
Explanation
DFS is usually written with recursion or a stack, BFS with a queue. Both visit every node once, O(V + E), but in a different order. Both work on trees and on general graphs.