GATE Computer ScienceVery hardGATE CS (PYQ pattern)

Iterative postorder uses two stacks: push the root on S1; repeat: pop x from S1, push x on S2, push x's left child then its right child on S1 (when non-null). Finally pop S2 completely to print. For a perfect binary tree with 15 nodes (4 levels), what is the maximum size S1 reaches (measured after the pushes of each iteration), and what is the size of S2 at the end?

  1. A. 5 and 15
  2. B. 4 and 8
  3. C. 4 and 15
  4. D. 8 and 15

Correct answer: C. 4 and 15

S1 behaves like the DFS stack: each pop adds two children, so its size grows by one per level descended; the maximum is 4. S2 receives every node once: 15.

More Programming and Data Structures (GATE) questions

Exams that ask this topic