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?
- A. 5 and 15
- B. 4 and 8
- C. 4 and 15
- 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.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- struct N { int v; int next; }; struct N a[6] = {{10,3},{20,5},{30,-1},{40,1},{50,2},{60,4}}; int i = 0, s = 0, k = 1; while (i != -1) { s…
- How many structurally distinct binary search trees on keys 1..7 have key 4 at the root and height exactly 3 (a single node has height 0)?
- char s = "abracadabra"; int c[26] = {0}, i; for (i = 0; s[i]; i++) c[s[i] - 'a']++; int best = 0; for (i = 1; i < 26; i++) if (c[i] (i +…
- For n = 7 distinct keys, what is the maximum number of swaps that the bottom-up (Floyd) build-heap for a max-heap can perform, over all…
- The stock span of day i is the number of consecutive days ending at i (including i) whose price is at most the price on day i. Prices are…
- The largest rectangle in a histogram with bar heights 2, 1, 5, 6, 2, 3, 4, 1, 3 (unit widths) is found with a stack of indices; a sentinel…