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)
  1. A. 20 35 40 30 60 80 70 50
  2. B. 20 30 35 40 50 60 70 80
  3. C. 50 30 20 40 35 70 60 80
  4. 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.

More Pseudocode and Code Tracing questions

Exams that ask this topic