Awesome q2a theme
0 votes

Depth first search is equivalent to which of the following traversal in Binary trees

  1. Pre – Order traversal
  2. Level Order traversal
  3. In – Order traversal
  4. Post – Order traversal
in Others by (178 points) | 22 views

1 Answer

0 votes

Option A is the correct answer

In PRE-ORDER traversal we first take the left node into consideration


In the case of DFS also, the same behaviour can be observed when it comes to the binary tree

by (419 points)
why not post order ?
yes, post-order will be ans. rt??

because root printed at last.
visiting the nodes is different than printing the nodes

U mean here visiting like post-order. but printing can be anything??

@srestha @shaik_masthan

Answer is PRE-ORDER 

In DFS  you can traverse the tree either from Left-to-right or right-to-left.


In case of left-to-right, you can follow the same method as PRE-Order because in preorder also we generally visit the node for the first time and then print it, which is same as the left-to-right traversal in DFS.

But, if you start traversing the tree in Post-order manner then, in case of leaf node it seems to be perfect but in case of internal nodes which have two children as leaf node, parent node will be visited first but not be printed then left node will be visited and printed after that, parent node will be printed and then right leaf node will be visited printed but this output will never match to the DFS traversal’s output because parent node should be printed first in order to match the output with DFS output, and this is possible only in case of PRE-ORDER.

932 questions
596 answers
81,474 users