recategorized by
26,729 views
70 70 votes

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

  1. LASTIN = LASTPOST
  2. LASTIN = LASTPRE
  3. LASTPRE = LASTPOST
  4. None of the above

6 Answers

Best answer
97 97 votes

Inorder : Left $\rightarrow$ Root $\rightarrow$ Right

Preorder : Root $\rightarrow$ Left $\rightarrow$ Right

Postorder: Left $\rightarrow$ Right $\rightarrow$ Root

If the binary tree is full (last level is fully filled), the last visited node in Inorder and Preorder must be the rightmost one in the last level. But for a complete binary tree this need not be the case (in a complete binary tree last level need not be fully filled) and LASTPRE will be from the second last level in case the complete binary tree is not full. So, choice (D).

edited by
27 27 votes
The answer is D.

Take any random sequence and check for the inorder, postorder and preorder Last Node.
6 6 votes

Answer: D. None of the above

We begin with a simple binary tree:

    A
   / \
  B   C
Preorder
RootLeftRight
ABC
LASTPRE = C
Inorder
LeftRootRight
BAC
LASTIN = C
Postorder
LeftRightRoot
BCA
LASTPOST = A

At this point, we observe that \( \text{LAST}{\text{IN}} = \text{LAST}{\text{PRE}} \).

Both Preorder and Inorder traversals typically end in the Right subtree. But if the tree lacks a right child, the equality no longer holds.

Consider:

 A
/
B
Preorder
RootLeftRight
AB 
LASTPRE = B
Inorder
LeftRootRight
BA 
LASTIN = A

\(\Rightarrow \text{LAST}{\text{IN}} \ne \text{LAST}{\text{PRE}}\)


🔽 Show another example (counterexample)

This is not the only example. If you notice, both sequences typically end with the Right subtree. The moment the last node in the tree does not have a right child, it serves as a counterexample.

      A
     / \
    B   C
   / \  /
  X   Y D
Preorder
RootLeftRight
AB X YC D
LASTPRE = D
Inorder
LeftRootRight
X B YAD C
LASTIN = C
Postorder
LeftRightRoot
X Y BD CA
LASTPOST = A

Now, \( \text{LAST}_{\text{IN}} \ne \text{LAST}_{\text{PRE}} \). The equality no longer holds because the Preorder traversal continues inside \( C \)’s subtree, whereas the Inorder traversal ends at \( C \).

Therefore, none of the equalities (A, B, or C) always hold for general binary trees.

✅ D. None of the above

edited by
3 3 votes

REVERSE APPROACH !!

Consider a Complete Binary Tree with only 1 node, say A,

Then its PREORDER = POSTORDER = INORDER = A

Therefore, only Option D matches (as Question ask, which always TRUE)

0 0 votes
Take a height 2 (root at 0) CBT, fill everything except the last node's last leaf, calculate in, pre and post orders, none of them match, now remove the last parent's one leaf too, now in and pre orders match, the key is to identify that they don't always match
Answer:
Position:
Show:

Related questions

51 51 votes
8 answers 8 answers
17.1k
17.1k views
Kathleen asked Sep 14, 2014
17,128 views
A multiset is an unordered collection of elements where elements may repeat any number of times. The size of a multiset is the number of elements in it, counting repetiti...
45 45 votes
11 answers 11 answers
17.9k
17.9k views
Kathleen asked Sep 14, 2014
17,927 views
Consider the following nested representation of binary trees: $(X \ Y \ Z)$ indicates $Y$ and $Z$ are the left and right subtrees, respectively, of node $X$. Note that $Y...
38 38 votes
2 answers 2 answers
9.6k
9.6k views
Kathleen asked Sep 14, 2014
9,642 views
Suppose a stack implementation supports, in addition to PUSH and POP, an operation REVERSE, which reverses the order of the elements on the stack.To implement a queue usi...
39 39 votes
5 answers 5 answers
9.9k
9.9k views
Kathleen asked Sep 14, 2014
9,852 views
A recursive program to compute Fibonacci numbers is shown below. Assume you are also given an array $f [ 0\ldots m]$ with all elements initialized to $0.$fib(n) { if (n ...