The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
0 votes

someone please explain this question 

A binary search tree contains the values 1, 2, 3, 4, 5, 6, 7, 8. The tree is traversed in pre-order and the values are printed out. Which of the following sequences is a valid output?



how the option D is correct?







D 53124768
asked in Study Resources by (41 points) | 182 views

2 Answers

0 votes
Best answer
  • We know the inorder of BST is sorted. So inorder is 1,2,3,4,5,6,7,8 

  • And preorder is given in option . With the help of inorder and preorder we construct a unique tree .

  • After constructing tree check it preorder is right or not .



answered by Boss (33.9k points)
edited by
pre order means .. parent left right .. so for this 5 3 1 2 4 7 86


you create tree like this  5 then 3 then on right side 7 ? why


where as pre order (parent left right )

it become 5 ,3 then on right side 1 ? why 7 ?

I think you are canfused how to create binary tree is preorder and inorder is given .

Look at the following steps



for OPTION A why not we create tree just like 2nd image .. where as you created 2nd image for option D 


@abhishekmehta4u  please responce

Amber you are right . I am doing silly mistakes. Now i am updationg soln
tree image would be like 2nd image only amber in option A but preorder sequence is wrong.!

and the first tree which you have made in your picture is not its wrong...6 should be on left to 7..
+1 vote

D is coorect  preorder traversal as you are given bst the inorder will be sorted i.e it will be 1,2,3,4,5,6,7,8

now when you take D option than tree which is traversed for preorder will be 


answered by Active (3.8k points)
edited by

Related questions

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
49,532 questions
54,123 answers
71,044 users