Question DSA-TREE-0061
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 build(pre, ino):
if not pre: return None
r = pre[0]; i = ino.index(r)
return N(r, build(pre[1:i + 1], ino[:i]), build(pre[i + 1:], ino[i + 1:]))
def post(n):
return post(n.l) + post(n.r) + [n.v] if n else []
t = build([3, 9, 20, 15, 7], [9, 3, 15, 20, 7])
print(post(t))
- A.
[3, 9, 20, 15, 7]
- B.
[9, 15, 7, 20, 3]
- C.
[9, 3, 15, 20, 7]
- D.
[15, 7, 20, 9, 3]
Sign up free to see the answer and a step-by-step explanation.