二叉搜索树中的每个结点,其左子树的所有结点值都小于该结点值,右子树的所有结点值都大于该结点 值。以下代码对给定的整数数组(假设数组中没有数值相等的元素),构造一个对应的二叉搜索树,横线上应填写( ):

uh269111 单选题 共 个子题
// 定义二叉树的结点结构
struct tree_node {
    int val;
    tree_node* left;
    tree_node* right;

    tree_node(int x) : val(x), left(nullptr), right(nullptr) {}
};

// 插入结点到二叉搜索树中
tree_node* insert(tree_node* root, int val) {
    if (root == nullptr) {
        return new tree_node(val);
    }

    ———————————————————————— // 在此处填入代码

    return root;
}

// 根据给定数组构造二叉搜索树
tree_node* constructBST(const int arr[], int size) {
    tree_node* root = nullptr;

    for (int i = 0; i < size; ++i) {
        root = insert(root, arr[i]);
    }
    return root;
}
A
if (val < root->val)
root->left = insert(root->left, val);
else
root->right = insert(root->right, val);
B
if (val > root->val)
root->left = insert(root->left, val);
else
root->right = insert(root->right, val);
C
if (val < root->val)
root->left = insert(root, val);
else
root->right = insert(root, val);
D
if (val > root->val)
root->left = insert(root, val);
else
root->right = insert(root, val);
正在加载题目数据…
已答 /
子题
该题型需在试卷/作业中作答,单题练习暂不支持
答案解析
讨论
发布
登录后参与讨论 立即登录
发布讨论需先绑定并验证手机号 去绑定手机号
加载中…
我
我
回复 @
回复 @
发送
还没有人讨论,来发表第一条吧