Question DSA-BST-159
HardWhat is the output of this code?
class N:
def __init__(self, v):
self.v, self.l, self.r = v, None, None
def ins(n, v):
if not n: return N(v)
if v < n.v: n.l = ins(n.l, v)
else: n.r = ins(n.r, v)
return n
def ino(n): return ino(n.l) + [n.v] + ino(n.r) if n else []
from collections import Counter
def count_shapes(keys):
import itertools
shapes = Counter()
for p in itertools.permutations(keys):
r = None
for v in p: r = ins(r, v)
(): n (sh(n.l), sh(n.r))
shapes[sh(r)] +=
(shapes)
(count_shapes([, , , ]))
- A.
14
- B.
10
- C.
Sign up free to see the answer and a step-by-step explanation.