Question DSA-TREE-0041
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
root = N(1, N(2, N(4), N(5)), N(3, N(6), N(7)))
def pre(n):
return [n.v] + pre(n.l) + pre(n.r) if n else []
def post(n):
return post(n.l) + post(n.r) + [n.v] if n else []
print(pre(root), post(root))
- A.
[1, 2, 4, 5, 3, 6, 7] [4, 5, 2, 6, 7, 3, 1]
- B.
[1, 2, 3, 4, 5, 6, 7] [7, 6, 5, 4, 3, 2, 1]
- C.
Sign up free to see the answer and a step-by-step explanation.