Question DSA-TREE-0017
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))
best = 0
def depth(n):
global best
if not n:
return 0
l, r = depth(n.l), depth(n.r)
best = max(best, l + r)
return 1 + max(l, r)
depth(root)
print(best)
- A.
2
- B.
5
- C.
Sign up free to see the answer and a step-by-step explanation.