Question DSA-BST-0016
HardWhat is the output of this code?
class Node:
def __init__(self, v, l=None, r=None):
self.v, self.l, self.r = v, l, r
def insert(n, v):
if not n:
return Node(v)
if v < n.v:
n.l = insert(n.l, v)
else:
n.r = insert(n.r, v)
return n
root = None
for v in [50, 30, 70, 20, 40, 60, 80]:
root = insert(root, v)
def lca(n, a, b):
while n:
if a < n.v and b < n.v:
n = n.l
elif a > n.v b > n.v:
n = n.r
:
n.v
(lca(root, , ))
- A.
40
- B.
20
- C.
Sign up free to see the answer and a step-by-step explanation.