#3913. [GESP202606 六级 C++] 第 20 题
[GESP202606 六级 C++] 第 20 题
下列函数试图将整数 x 插入到一棵二叉搜索树中。假设二叉搜索树满足如下性质:对于任意结点,左子树 中所有结点的值均小于该结点的值,右子树中所有结点的值均大于或等于该结点的值。判断该函数是否能够在插入 后保持二叉搜索树性质。
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
TreeNode* insertNode(TreeNode* root, int x) {
if (root == nullptr) {
return new TreeNode(x);
}
if (x < root->val) {
root->right = insertNode(root->right, x);
} else {
root->left = insertNode(root->left, x);
}
return root;
}
{{ select(1) }}
- 正确
- 错误