The inorder and preorder traversal of a binary tree are d b e a f c g ...
Solution:
Given:
Inorder traversal: d b e a f c g
Preorder traversal: a b d e c f g
Step 1: Identify the root node
From the preorder traversal, we know that the first element is the root node. So, we can see that the root node is 'a'.
Step 2: Identify the left and right subtrees
From the inorder traversal, we can see that the left subtree of 'a' is 'd b e' and the right subtree is 'f c g'.
From the preorder traversal, we can see that the left subtree of 'a' is 'b d e' and the right subtree is 'c f g'.
Step 3: Recursively apply steps 1 and 2 to the left and right subtrees
For the left subtree:
From the inorder traversal, we can see that the root node is 'b'.
From the preorder traversal, we can see that the root node is 'b'.
So, the left subtree of 'b' is 'd' and the right subtree is 'e'.
Similarly, for the right subtree of 'b':
From the inorder traversal, we can see that the root node is 'e'.
From the preorder traversal, we can see that the root node is 'e'.
So, the left subtree of 'e' is empty and the right subtree is empty.
We can continue this process for all the nodes until we reach the leaves.
Step 4: Postorder traversal
The postorder traversal is obtained by visiting the nodes in the following order: left subtree, right subtree, root node.
Using this order, we can obtain the postorder traversal of the binary tree:
d, e, b, f, g, c, a
Therefore, the correct answer is option C.
To make sure you are not studying endlessly, EduRev has designed Computer Science Engineering (CSE) study material, with Structured Courses, Videos, & Test Series. Plus get personalized analysis, doubt solving and improvement plans to achieve a great score in Computer Science Engineering (CSE).