Let LASTPOST, LASTIN, and LASTPRE denote the last vertex visited in postorder,…

2000

Let LASTPOST, LASTIN, and LASTPRE denote the last vertex visited in postorder, inorder, and preorder traversal respectively, of a complete binary tree. Which of the following is always true?

  1. A.

    LASTIN = LASTPOST

  2. B.

    LASTIN = LASTPRE

  3. C.

    LASTPRE = LASTPOST

  4. D.

    None of the above

Attempted by 41 students.

Show answer & explanation

Correct answer: D

Use the following complete binary tree as a counterexample:

        A
      /   \
     B     C
    / \   /
   D   E F

Inorder: D B E A F C, so LASTIN = C.

Postorder: D E B F C A, so LASTPOST = A.

Preorder: A B D E C F, so LASTPRE = F.

Thus LASTIN ≠ LASTPOST, LASTIN ≠ LASTPRE, and LASTPRE ≠ LASTPOST. Therefore none of the first three statements is always true.

Correct answer: None of the above

Explore the full course: Gate Guidance By Sanchit Sir