Question DSA-TREE-0011
MediumWhat 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 leaves(n):
if not n:
return 0
if not n.l and not n.r:
return 1
return leaves(n.l) + leaves(n.r)
print(leaves(root))
- A.
4
- B.
2
- C.
3
- D.
5
Sign up free to see the answer and a step-by-step explanation.