This problem is similar to the PreOrder + InOrder = PostOrder problem.
Consider the following binary tree :
A Input / \ InOrder = CBDAEGF B E PostOrder = CDBGFEA / \ \ C D F Output / PreOrder = ABCDEFG GYou are required to print the PreOrder traversal of the tree, given its InOrder and PostOrder traversals.
left subtree - root - right subtreeThe basic form of a PostOrder traversal is :
left subtree - right subtree - rootWe know that the root of the tree is the last node of the PostOrder traversal. In the InOrder traversal, this same node is located between the left subtree and the right subtree. Now, we can determine the left subtree and the right subtree in the InOrder traversal, and in the PostOrder traversal as well, since they have the same number of nodes in it.
To print the PreOrder traversal, we find the root of the tree, print it, and recursively find the root of the left and the right subtree.
![]() |
![]() |