Consider the In-order and Post-order traversals of a tree as given below:
In-order: j e n k o p b f a c l g m d h i
Post-order: j n o p k e f b c l m g h I d a
The Pre-order traversal of the tree shall be
Show the worked solutionThe traversals are of a general (ordered) tree, where inorder means: first subtree in inorder, then the root, then the remaining subtrees in inorder.
The last node in postorder, a, is the root. In the inorder, a splits the nodes into the first subtree \(\{j,e,n,k,o,p,b,f\}\) and the rest \(\{c,l,g,m,d,h,i\}\).
Repeating this on each part gives the children lists\[a:(b,c,d),\ b:(e,f),\ e:(j,k),\ k:(n,o,p),\ d:(g,h,i),\ g:(l,m).\]Check: postorder of this tree is j n o p k e f b c l m g h i d a, as given.
Preorder (root, then children left to right):\[a\ b\ e\ j\ k\ n\ o\ p\ f\ c\ d\ g\ l\ m\ h\ i.\]Answer: C
GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com