← 返回题库
中级

二叉搜索树-插入

未完成
中级参考 代码结构已给出,请填写 ____ 处
class BSTNode:
    def __init__(____):
        self.val = val
        self.left = None
        self.right = None

def bst_insert(____):
    if root is None:
        return BSTNode(____)
    if val < root.val:
        root.left = bst_insert(____)
    else:
        root.right = bst_insert(____)
    return root

示例

输入
root = None; root = bst_insert(root, 5); root = bst_insert(root, 3); root = bst_insert(root, 7); print(root.val, root.left.val, root.right.val)
期望输出
5 3 7
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码