64,647
社区成员
发帖
与我相关
我的任务
分享
// 删除以t为根的BST中值为x的节点
void remove(int x, BinaryNode*& t)
{
if ( t == NULL)
{
return ;
}
if (x < t->data)
{
remove(x, t->left);
}
else if (x > t->data)
{
remove(x, t->right);
}
// 左右都有节点的情况
else if (t->left != NULL && t->right != NULL)
{
t->data = findMin(t->right)->data; // 右子树最小的节点
remove(t->data, t->right);
}
else
{
BinaryNode* oldNode = t;
t = (t->left != NULL) ? t->left : t->right);
delete oldNode;
}
}