ProgrammingVery hardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
// BST: smaller keys go to the left, larger to the right
Insert into an empty BST, in this order: 50, 30, 70, 20, 40, 60, 80, 35
Procedure post(Node t)
if (t == NULL)
return
end if
post(t.left)
post(t.right)
Print t.key
End Procedure
post(root)- A. 20 35 40 30 60 80 70 50
- B. 20 30 35 40 50 60 70 80
- C. 50 30 20 40 35 70 60 80
- D. 20 40 35 30 60 80 70 50
Correct answer: A. 20 35 40 30 60 80 70 50
The tree: 50 at the root; 30 (children 20 and 40, with 35 as the left child of 40) on the left; 70 (children 60, 80) on the right. Post-order visits left subtree, right subtree, then the node: 20 35 40 30 60 80 70 50.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? Function ways(Integer n) if (n < 0) return 0 end if if (n == 0) return 1 end if…
- What will be the output of the following pseudocode? (substring(s, i, k) is the k characters starting at index i; / is integer division; +…
- What will be the output of the following pseudocode? Function f(Integer n) if (n == 0) return 0 end if if (n == 1) return 1 end if return…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[8] = {2, 0, 1, 2, 1, 0, 2, 1} Integer lo = 0…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[7] = {7, 2, 9, 4, 1, 8, 5} Integer pivot =…
- What will be the output of the following pseudocode? Integer a[9] = {3, 3, 4, 2, 4, 4, 2, 4, 2} Integer cand = 0, cnt = 0 for (each i from…