Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use BFS, DFS for tree traversal or in-order, post -order, pre-order?

This question may be simple for experts but for a beginner like me it is important. My question is are there any problems involving tree traversals that can be solved by BFS , DFS and not in-order, pre-order etc. In other words, whenever i see a tree problem, should I ONLY think of the 3 tree traversal methods, or also consider BFS,DFS

like image 767
Programmer Avatar asked Dec 24 '10 15:12

Programmer


People also ask

Is it wrong to apply for the same job twice?

Yes, you should absolutely apply for the role again. There are so many factors as to why you didn't get the job or interview. By the time you applied they might have already been in the final stages of the interview with their ideal candidate but then the candidate backed out.

How long should you wait before applying for the same job again?

But how long should you wait before applying for the same job again? You should wait at least 3 months before applying for the same job again. If the position is still open at this time, it's a good reminder that you are eager for the role. If the application closed then opened up again, don't hesitate to re-apply.

How long do you have to wait to reapply at Facebook?

If you've had 1 rejection, you must wait 7 days until you can reapply. If you've had 2 rejections, you must wait 21 days until you can reapply.

Can you apply to a company more than once?

There's no reason not to apply to the same company twice. Just be sure to go about it strategically. Sometimes, we set our sights on certain companies to work for, only to have those same companies reject us the first time we apply.


2 Answers

Pre-order, in-order and post-order traversal are the three different kinds of depth first search that are possible. So it's not a question of whether to use DFS or one of those three. If you are using one of those three traversals, you are using DFS.

As for whether there are cases where BFS is preferable over DFS: Yes, there are. For example to find the shortest path between two nodes in an unweighted graph, you can use BFS because the first path found by a BFS happens to be the one with the fewest edges. The same is not true for DFS.

like image 88
sepp2k Avatar answered Oct 07 '22 07:10

sepp2k


An obvious example where DFS doesn't work and you have to use BFS is an infinitely (or at least arbitrarily) high tree.

like image 43
Jörg W Mittag Avatar answered Oct 07 '22 05:10

Jörg W Mittag