Question DSA-BST-136
MediumWhat is the output of this code?
class N:
def __init__(self, v):
self.v, self.l, self.r = v, None, None
def ins(n, v):
if not n:
return N(v)
if v < n.v:
n.l = ins(n.l, v)
else:
n.r = ins(n.r, v)
return n
r = None
for v in [8, 3, 10, 1, 6, 14, 4, 7, 13]:
r = ins(r, v)
def kth(n, k):
st = []
while True:
while n:
st.append(n); n = n.l
n = st.pop()
k -=
k == :
n.v
n = n.r
(kth(r, ))
- A.
6
- B.
4
- C.
Sign up free to see the answer and a step-by-step explanation.