Search code examples
algorithmtreelanguage-agnostictraversaltree-traversal

Is post-order traversal == bottom-up traversal and pre-order traversal == top-down traversal?


Would it be right to say post-order traversal of a tree should be used to do a bottom-up traversal whereas a pre-order traversal should be used to do a top-down traversal of a binary tree?

This has been true in all the examples that I have encountered, so I just wanted to confirm. There are some problems for which it is intuitive to start from the leaves (at the bottom). Can we use post-order traversal to solve them (and vice versa)?

Thanks!


Solution

  • No. While post-order traversal and pre-order traversal have clear definition, bottom-up traversal or top-down traversal terms might be interpreted by different ways, they are not generally accepted for binary trees. If anybody uses last terms for trees, exact meaning depends on context.

    Look at the picture from wiki:

    enter image description here

    Does pre-order traversal here look like top-down traversal? Or post-order likes bottom-up traversal? Seems no.

    Perhaps you want to consider BFS methods to get level order