Consider the following Inorder and Preorder traversal of a tree Inorder – D B E F A G H C Preorder - A B D E F C G H What is the Postorder Traversal of the same tree
Consider the following Inorder and Preorder traversal of a tree Inorder – D B E F A G H C Preorder - A B D E F C G H What is the Postorder Traversal of the same tree Correct Answer D F E B H G C A
Concept:
- Inorder and Preorder = Unique Binary tree
- Inorder and Postorder = Unique Binary tree
- Preorder and Postorder = More than one Binary tree
- Inorder Traversal: Left -> Root -> Right
- Preorder Traversal: Root -> Left -> Right
- Post Order Traversal : Left -> Right -> Root
Algorithm: (For Inorder and Preorder to make Binary tree)
Step 1: Pick a node in the same sequence in the Preorder
Step 2: Make left and right of picked node from Inorder
Step 3: Repeat Step 1
Step 4: Repeat Step 2, Loop till the last element in pre order
Explanation:
Binary tree after using this algorithm on pre order and in order:
[ alt="F2 R.S 20.5.20 Pallavi D5" src="//storage.googleapis.com/tb-img/production/20/05/F2_R.S_20.5.20_Pallavi_D5.png" style="width: 206px; height: 181px;">
Final Binary tree
[ alt="F2 R.S 20.5.20 Pallavi D6" src="//storage.googleapis.com/tb-img/production/20/05/F2_R.S_20.5.20_Pallavi_D6.png" style="width: 201px; height: 186px;">
Using Concept left-> right -> root for post order
Post order = D F E B H G C A
Hence option 3 is the correct answer.