The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+19 votes
2.3k views

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
asked in DS by Veteran (59.5k points)
recategorized by | 2.3k views
+2
  • A full binary tree (sometimes proper binary tree or 2-tree) is a tree in which every node other than the leaves has two children. ?
  • A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible. ?
0
if this is complete then, what is almost complete...?
0
actually various textbook follows different definition of complete and full binary tree ,
0

@vishalsharma539

  1. Complete Binary tree : A binary tree is complete if all level except possibly the last level is completely full, i.e last level may be or may not be full.
  2. Perfect Binary tree : A complete binary tree in which last level is full.
  3. Almost complete binary tree : A almost complete binary tree is a binary tree which is complete binary tree but not perfect binary tree.

2 Answers

+26 votes
Best answer

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).

answered by Veteran (353k points)
edited by
+26

I guess this is also a Complete Binary Tree. And the question has asked for the condition which is always true. In some cases what you said may not be true as the above one.

+2
yeah right thanks !!!
+4
Yes. You are correct. I took "full" instead of "complete".
+1
Arjun Sir, Can you please explain the difference between full and complete binary tree as different sources have different binary trees
+3
Please update this answer.
+1

This is copied from Rosen, and the defiition of complete Binary Tree is different than yours.
the tree which you are calling as Full is same as Complete tree.
answer should be (B), right?

+1
As far as I know, if the definition is not mentioned, then a complete binary tree is one in which nodes are present sequentially(i.e. following the property of Almost Complete Binary tree) and a node can have either 0 or 2 children. A node should'nt have just a single child thus !!
0

@Arjun sir Can you please share an example of this question. Because below tree is Not Complete Binary Tree ; it is almost Complete Binary tree.

https://gateoverflow.in/?qa=blob&qa_blobid=6157632728430700751

+2
A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible. This is 100% correct.
+1
@Arjun sir,In answer you mentioned that :-

 LASTPRE will be from the second last level in case the complete binary tree is not full. So, choice (D).

LastIn should be from second lastindex in case last level is completely filled.LASTPRE will always be from last level.because if we are at second last level we process ROOT and then goto last level in pre order.Please check once?
+20 votes
The answer is D.

Take any random sequence and check for the inorder, postorder and preorder Last Node.
answered by Boss (19.7k points)
+4
dont you think it should be (B) ?
0
Look at my comment below for explanation.
+2
always true for full binary tree ..but not for complete !
0
Yes for full binary tree it is option B
0

Here, you have to be careful. It Says, "Complete binary tree" for which every level except last is completely filled and at last level, nodes are as far as left possible.



Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

37,111 questions
44,694 answers
127,234 comments
43,753 users