Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
When can BFS be useful? For me, it looks like a more complicated version of DFS.

evmorov

Jan 25, 2023

When can BFS be useful? For me, it looks like a more complicated version of DFS.

0

0

Comments
Comments
M
mariana.lopez.jaimez 2 years ago

I've read two arguments in favor of using BFS over DFS:

  1. You need to get/process/print the nodes of a tree or graph "by level". I don't have a practical example in mind but I think of this as "onion-peeling" a graph.
  2. The graph or tree's height/depth is shallow and...
Divyanshu Varma
Divyanshu Varma6 months ago

BFS is used when you -

  • need to get values "in a wave" or in other words, level by level. This applies to graphs and therefore trees too.
  • if you think about it, you will get the closest distance to any node from BFS as well (provided graph is unweighted, if weighted...

On this page

Problem Statement

Try it yourself