Is it possible the pre-order traversal be the same order as the post-order traversal?

Omid7 picture Omid7 · Apr 7, 2013 · Viewed 7.8k times · Source

If T be an ordered tree with more than one node. Is it possible the pre-order traversal of T visit the nodes in the same order as the post-order traversal of T? if "yes" can you please give an example. And if "No" could you please explain why it cannot occur?

Answer

ggbranch picture ggbranch · Apr 7, 2013

Unless I'm missing something painfully obvious, the answer would be no. A ordered tree with > 1 node (say for example, 2 nodes) will look like this.

    A    
 B                     

or

    A
        C

Post-order traversal visits the nodes in the order left-right-root and pre-order visits the nodes in the order of root-left-right. In order for them to produce the same output, "left" must be equals to "root", which just doesn't make sense. With the above example, pre-order will produce AB or AC respectively and post-order will produce BA and CA.