Question DSA-TREE-0045
MediumWhat is the output of this code?
class N:
def __init__(self, v, l=None, r=None):
self.v, self.l, self.r = v, l, r
def lca(n, a, b):
if not n or n.v in (a, b):
return n
L, R = lca(n.l, a, b), lca(n.r, a, b)
return n if L and R else L or R
t = N(3, N(5, N(6), N(2, N(7), N(4))), N(1, N(0), N(8)))
print(lca(t, 7, 4).v, lca(t, 6, 4).v, lca(t, 5, ).v)
- A.
2 5 3
- B.
7 5 1
- C.
Sign up free to see the answer and a step-by-step explanation.