Question DSA-TREE-0064
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 is_bal(n):
def h(n):
if not n: return 0
a, b = h(n.l), h(n.r)
if a < 0 or b < 0 or abs(a - b) > 1: return -1
return 1 + max(a, b)
return h(n) >= 0
t1 = N(1, N(2, N(4)), N(3))
t2 = N(1, N(, N(, N())), N())
(is_bal(t1), is_bal(t2))
- A.
True True
- B.
True False
- C.
Sign up free to see the answer and a step-by-step explanation.