in Programming edited by
120 views
1 vote
1 vote

Elements $1,2,3,4,5$ are inserted in a stack in sequence. The elements are printed while popping them out. Which of the following is a wrong print sequence?

  1. $3 \ 5 \ 4 \ 2 \ 1$
  2. $1 \  3 \ 2 \ 4 \ 5$
  3. $5 \ 4 \ 3 \ 2 \ 1$
  4. $2 \ 4 \ 1 \ 5 \ 3$
in Programming edited by
by
120 views

3 Comments

i think here A and C is corrrect.........but not B.........so its A option is correct plz check??
1
1

yes, you are right, it had multiple answers correct A and C. 

 source - http://www.geeksforgeeks.org/if-you-are-given-two-traversal-sequences-can-you-construct-the-binary-tree/

Changed that question as that had multiple answers and also was one line question.

1
1
please provide a solution, I'm noting this. The link given is also something else, not related to question.
0
0

1 Answer

1 vote
1 vote
Best answer
Elements $1,2,3,4,5$ are inserted in a stack in sequence.

For option $(A)$: push(1),push(2),push(3),pop,push(4),push(5),pop,pop,pop,pop$\implies3,5,4,2,1$

For option $(B)$: push(1),pop,push(2),push(3),pop,pop,push(4),pop,push(5),pop$\implies1,3,2,4,5$

For option $(C)$: push(1),push(2),push(3),push(4),push(5),pop,pop,pop,pop,pop$\implies5,4,3,2,1$

For option $(D)$: push(1),push(2),pop,push(3),push(4),pop after that we required $1$,but we can pop only $3$.And if we push(5),then we get $5$.

So, $2,4,1,5,3$ is not got at any operation we performed.

So,the correct answer is $(D)$.
selected by
Answer:

Related questions