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?

  1. A. 7
  2. B. 3
  3. C. 4
  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.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic