插入 20 之后它挂在谁下面

👁️ 0 人浏览 💬 0 人评论 ❤️ 添加收藏

还是那棵树,再插一个 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)
提交你的答案
请登录后提交答案。
去登录
代码编辑器
Ctrl + Enter 运行
本次输入:
输出:

                        
👩‍🏫
AI
💬 题目评论

全部评论