Question DSA-TREE-0062
HardWhat 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 max_path(n):
best = float("-inf")
def go(n):
nonlocal best
if not n: return 0
l, r = max(go(n.l), 0), max(go(n.r), 0)
best = max(best, n.v + l + r)
return n.v + max(l, r)
go(n)
return best
t = N(-10, N(9), N(20, N(15), N(7)))
print(max_path(t))
- A.
35
- B.
34
- C.
Sign up free to see the answer and a step-by-step explanation.