Search code examples
treebinary-treetree-traversal

Tree Traversal question- A textbook question


This is a question from a textbook with the answer provided about a binary tree.

If the postorder traversal visits the nodes of a binary tree storing character values in the order of U, G, T, R, A, I, what is the visit order for an inorder traversal of the same binary tree?

a) I, G, U, A, T, R

b) R, G, U, I, T, A

c) G, U, I, T, A, R

d) cannot be determined

Answer: c


Now the question is how is C the answer. I realize a postorder traversal alone is not enough to uniquely id a tree and a pre order and postorder traversal can be defined for any kind of tree (not inorder) but cannot uniquely id a tree together. An inorder and postorder traversal can. The question does not specifiy a BST so that can make a difference. So I guess I need clarificaton on how the answer about is C rather than D which is what I think it is.


Solution

  • I agree with your assessment.

    Although it is true that option C is compatible with the given post-order traversal -- when we take this tree:

       I                Inorder: G U I T A R
     /   \              Postorder: U G T R A I
    G     A
     \   / \
      U T   R
    

    ...option A is also compatible with the given post order, if we take this tree:

        I               Inorder: I G U A T R
         \              Postorder: U G T R A I
          A
        /   \
       G     R
        \   /
         U T
    

    As you rightly indicate, a post order traversal alone does not uniquely define a binary tree, and the only right answer is that the in-order traversal cannot be determined (option D).

    Unless the original question has additional information, it is a bad question.