Is Pre-Order traversal on a binary tree same as Depth First Search?

前端 未结 4 1030
情书的邮戳
情书的邮戳 2020-12-24 02:18

It seems to me like Pre-order traversal and DFS are same as in both the cases we traverse till the leaf node in a depth wise fashion. Could anyone please correct me if I am

4条回答
  •  粉色の甜心
    2020-12-24 02:36

    Intuitively, they feel the same due to the way we apply DFS algorithm using recursion (i.e making use of implicit stack data structure) in most of the implementations. In most of the cases (or all) the results are the same as for a Pre-Order Traversal.

    The idea of DFS is to pick a branch and go deep into that, explore it completely. Whereas, the fashion of picking the branch and going down is determined by what kind of DFS you are implementing. Just for the sake of completion, in BFS algorithm, we traverse level-wise.

    Remember, Pre-Order is just a type of DFS. We also have other methods which are shown below.

    Image Source: Base CS

    For better understanding, refer this blog or even this one.

提交回复
热议问题