Question DSA-BST-156
HardWhat 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
def ino(n): return ino(n.l) + [n.v] + ino(n.r) if n else []
def lca(n, a, b):
while n:
if a < n.v and b < n.v: n = n.l
elif a > n.v and b > n.v: n = n.r
else: return n.v
r = None
v [, , , , , , ]:
r = ins(r, v)
(lca(r, , ), lca(r, , ), lca(r, , ))
- A.
8 8 20
- B.
10 8 22
- C.
Sign up free to see the answer and a step-by-step explanation.