edited by
1,863 views
3 votes
3 votes

how can we find unique structure of a tree with given pre order and post order traversal . Please explain in detail with an example ( please give an example with number of nodes > 5 for more understanding )

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

https://gateoverflow.in/50608/generate-unique-binary-order-level-order-traversal-explain

Says that no unique tree can be constructed .  But in VIT 2016 admission Test there had a question from this Topic .
Could some one help ? Is it Really not possible ?

edited by

1 Answer

Best answer
7 votes
7 votes
NO with only preorder and post order given we cant find a unique tree....
either preorder with inorder or postorder with inorder is required for "unique" tree..

 we have 2nCn/(n+1) binary trees with n nodes(unlabelled).....and we can get a pre order for each one of them.....and if we try to find post order then definitly we will get more than 1 tree satisfying both conditions together

we need inorder also....either we use inorder with pre or post we will get unique tree each time....but it is neccessary to have inorder..because then only 1 tree can satisfy these conditions

let me give an example with 3 nodes..u can generalise it afterwards

say we have three nodes...even if labelled we get 5 binary trees and every binary tree can be a preorder....
preorder ABC..we get 5 trees..
suppose we want post order CBA....u will see we will get 4 out of those 5 trees satisfying both preorder and post order simultaneously
so UNIQUE TREE not possible with only pre and post order given
try to apply inorder condition sayBCA...
only one out of those 4 trees will satisfy all of them together..
selected by

Related questions

0 votes
0 votes
0 answers
2
Vikas123 asked Jan 8, 2019
729 views
Acc. to (question) my solution is...uniquely constructed binary tree PRE+POST and IN+POST…where i am wrong….
1 votes
1 votes
0 answers
3
iarnav asked Jan 6, 2018
642 views
WHAT IS THE POST ORDER IF ROOT NODE IS P?
0 votes
0 votes
2 answers
4