Question DSA-TREE-0063
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
from collections import deque
def zigzag(n):
out, q, lr = [], deque([n]), True
while q:
lvl = [x.v for x in q]
out.append(lvl if lr else lvl[::-1]); lr = not lr
for _ in range(len(q)):
x = q.popleft()
for c in (x.l, x.r):
if c: q.append(c)
return out
t = N(1, N(2, N(4), N(5)), N(3, N(), N()))
(zigzag(t))
- A.
[[1], [3, 2], [4, 5, 6, 7]]
- B.
[[1], [2, 3], [4, 5, 6, 7]]
- C.
Sign up free to see the answer and a step-by-step explanation.