The preorder traversal of a binary search tree is 15, 10, 12, 11, 20, 18, 16, 19. Which one of the following is the postorder traversal of the tree ?
(A) 10, 11, 12, 15, 16, 18, 19, 20
(B) 11, 12, 10, 16, 19, 18, 20, 15
(C) 20, 19, 18, 16, 15, 12, 11, 10
(D) 19, 16, 18, 20, 11, 12, 10, 15
Answer: (B)
Explanation: Please refer – Find postorder traversal of BST from preorder traversal
// C++ program for finding postorder // traversal of BST from preorder traversal #include <bits/stdc++.h> using namespace std; // Function to find postorder traversal from // preorder traversal. void findPostOrderUtil(int pre[], int n, int minval, int maxval, int& preIndex) { // If entire preorder array is traversed then // return as no more element is left to be // added to post order array. if (preIndex == n) return; // If array element does not lie in range specified, // then it is not part of current subtree. if (pre[preIndex] < minval || pre[preIndex] > maxval) { return; } // Store current value, to be printed later, after // printing left and right subtrees. Increment // preIndex to find left and right subtrees, // and pass this updated value to recursive calls. int val = pre[preIndex]; preIndex++; // All elements with value between minval and val // lie in left subtree. findPostOrderUtil(pre, n, minval, val, preIndex); // All elements with value between val and maxval // lie in right subtree. findPostOrderUtil(pre, n, val, maxval, preIndex); cout << val << " "; } // Function to find postorder traversal. void findPostOrder(int pre[], int n) { // To store index of element to be // traversed next in preorder array. // This is passed by reference to // utility function. int preIndex = 0; findPostOrderUtil(pre, n, INT_MIN, INT_MAX, preIndex); } // Driver code int main() { int pre[] = { 15, 10, 12, 11, 20, 18, 16, 19 }; int n = sizeof(pre) / sizeof(pre[0]); // Calling function findPostOrder(pre, n); return 0; } |
chevron_right
filter_none
Code – https://ide.geeksforgeeks.org/t49kAtJwr7
Option (B) is correct.
Quiz of this Question
Recommended Posts:
- GATE | GATE CS 2020 | Question 40
- GATE | Sudo GATE 2020 Mock III (24 January 2019) | Question 15
- GATE | Sudo GATE 2020 Mock III (24 January 2019) | Question 16
- GATE | Sudo GATE 2020 Mock III (24 January 2019) | Question 17
- GATE | Sudo GATE 2020 Mock III (24 January 2019) | Question 18
- GATE | Sudo GATE 2020 Mock III (24 January 2019) | Question 19
- GATE | Sudo GATE 2020 Mock III (24 January 2019) | Question 20
- GATE | Sudo GATE 2020 Mock III (24 January 2019) | Question 21
- GATE | Sudo GATE 2020 Mock III (24 January 2019) | Question 22
- GATE | Sudo GATE 2020 Mock I (27 December 2019) | Question 47
- GATE | Sudo GATE 2020 Mock I (27 December 2019) | Question 48
- GATE | Sudo GATE 2020 Mock I (27 December 2019) | Question 49
- GATE | Sudo GATE 2020 Mock II (10 January 2019) | Question 22
- GATE | Sudo GATE 2020 Mock I (27 December 2019) | Question 1
- GATE | Sudo GATE 2020 Mock I (27 December 2019) | Question 2
- GATE | Sudo GATE 2020 Mock I (27 December 2019) | Question 3
- GATE | Sudo GATE 2020 Mock I (27 December 2019) | Question 4
- GATE | Sudo GATE 2020 Mock I (27 December 2019) | Question 5
- GATE | Sudo GATE 2020 Mock I (27 December 2019) | Question 6
- GATE | Sudo GATE 2020 Mock I (27 December 2019) | Question 7

