Data Structures & AlgorithmsHardInfosys · Accenture pattern
Keys 50, 30, 20, 10, 40, 70, 60 are inserted into an empty BST. An iterative inorder traversal pushes nodes while walking left and pops to visit. What is the MAXIMUM number of nodes on the stack at any moment?
- A. 7
- B. 3
- C. 4
- D. 2
Correct answer: C. 4
The stack holds the chain of ancestors whose left subtrees are still being visited. Initially it pushes 50, 30, 20, 10 (the left spine) = 4. Later chains are shorter: after visiting 30 the stack holds 50, 40; after 50 it holds 70, 60. So the maximum is 4, i.e. O(h) space, not O(n) = 7.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- A course plan has prerequisites (a → b means a must come before b): 1→3, 2→3, 3→5, 4→5, 5→6, 2→4, 6→7. Any number of courses with all…
- Flights (directed, cost): 0→1 100, 1→2 100, 2→3 100, 0→2 500, 1→3 600, 0→3 900. What is the cheapest cost from 0 to 3 using AT MOST 1…
- What is the diameter (largest distance between any two nodes) of the weighted tree with edges 1–2 (3), 1–3 (4), 2–4 (2), 2–5 (6), 3–6 (5)…
- Running median with two heaps (a max-heap for the lower half, a min-heap for the upper half, sizes differing by at most 1, the lower half…
- Each array is a binary tree in level order (0-indexed, children at 2i + 1 and 2i + 2, all positions present). Which one is a valid BST?
- Binary trees with node labels A, B, C have preorder A B C and postorder C B A. How many different binary trees match BOTH traversals?