插入 20 之后它挂在谁下面
还是那棵树,再插一个 20。运行下面这段程序:
class BNode:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def insert(root, val):
if root is None:
return BNode(val)
if val < root.val:
root.left = insert(root.left, val)
elif val > root.val:
root.right = insert(root.right, val)
return root
def build():
root = None
for v in [17, 24, 15, 13, 23]:
root = insert(root, v)
return root
root = build()
cur = root
while True:
nxt = cur.left if 20 < cur.val else cur.right
if nxt is None:
break
cur = nxt
print(cur.val)
全部评论