Question DSA-BST-0007
EasyWhat is the output of this code?
class Node:
def __init__(self, v, l=None, r=None):
self.v, self.l, self.r = v, l, r
def insert(n, v):
if not n:
return Node(v)
if v < n.v:
n.l = insert(n.l, v)
else:
n.r = insert(n.r, v)
return n
root = None
for v in [50, 30, 70, 20, 40, 60, 80]:
root = insert(root, v)
def find(n, v):
while n and n.v != v:
n = n.l if v < n.v else n.r
return n
(find(root, ))
- A.
False
- B.
True
- C.
Show answer and explanation
Answer: B
Searching for 60: 60 > 50 goes right to 70, 60 < 70 goes left to 60 — found.