Question DSA-TREE-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
root = Node(1, Node(2, Node(4), Node(5)), Node(3))
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
print(lca(root, 4, 5).v)
- A.
4
- B.
2
- C.
1
- D.
5
Sign up free to see the answer and a step-by-step explanation.