243 views
1 1 vote

A unique Binary Search Tree (BST) is constructed using the sequence of keys $[45,20,10,30,80,60,90,70]$. If we perform a non-recursive (iterative) In-order traversal of this tree using an auxiliary stack, what is the maximum number of elements present in the stack at any point during the execution?

  1. $3$
     
  2. $5$
     
  3. $8$
     
  4. $4$

1 Answer

1 1 vote

X means there is no left node of $60$.

In an iterative in-order traversal using a stack, the maximum stack size corresponds to the maximum depth (or height) of the tree being explored. We push all ancestors of a node onto the stack as we travel down to the leftmost child.

  • Path $\mathbf{1}: 45 \rightarrow 20 \rightarrow 10$ (Stack size $3: [45,20,10])$.
     
  • Path $\mathbf{2}: 45 \rightarrow 80 \rightarrow 60 \rightarrow 70$ (Stack size $4: [45,80,60,70])$

When the traversal reaches the leaf node $\mathbf{7 0}$, the stack contains all its ancestors that are still being processed. This results in the maximum stack size.

Correct Option: D. $\mathbf{4}$

Answer:
Position:
Show:

Related questions

0 0 votes
1 1 answer
142
142 views
GO Classes asked Feb 4
142 views
In a hash table with $m$ slots, collisions are resolved using linear probing. The hash function is $h(k)=k \bmod m$. If the current state of the table contains keys at in...
1 1 vote
1 1 answer
140
140 views
GO Classes asked Feb 4
140 views
Analyze the following code snippet:x = 5 def outer_func(): x = 10 def inner_func(): global x x += 1 print(x, end=" ") inner_fu...
1 1 vote
1 1 answer
138
138 views
GO Classes asked Feb 4
138 views
Consider the following Python function definition and calls:def append_to_list(val, items=[]): items.append(val) return items list1 = append_to_list(10) list2...
1 1 vote
1 1 answer
157
157 views
GO Classes asked Feb 4
157 views
Consider the following Python code snippet:def create_functions(): funcs = [] for i in range(3): funcs.append(lambda x: x * i) return funcs functions ...