This problem is similar to the InOrder + PostOrder = PreOrder problem.
Consider the following binary tree :
A Input / \ PreOrder = ABCDEFG B E InOrder = CBDAEGF / \ \ C D F Output / PostOrder = CDBGFEA GYou are required to print the PostOrder traversal of the tree, given its PreOrder and InOrder traversals.
root - left subtree - right subtreeThe basic form of an InOrder traversal is :
left subtree - root - right subtreeWe know that the root of the tree is the first node of the PreOrder 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 PreOrder traversal as well, since they have the same number of nodes in it.
To print the PostOrder traversal, we find the root of the tree, recursively find the root of the left and the right subtree, and finally print the root of the current tree.
![]() |
![]() |