方法一:迭代
1.1 findMin
class Solution
{
public:
int findMin(TreeNode* root)
{
while (root->left != nullptr)
{
root = root->left;
}
return root->val;
}
TreeNode* deleteNode(TreeNode* root, int key)
{
TreeNode* dummy = new TreeNode{ -1,root,root }; //定义一个dummy节点是为了方便删除根节点
TreeNode* parent = dummy;
TreeNode* child = root;
if (root == nullptr)
{
return nullptr;
}
while (child != nullptr && key != child->val)
{
parent = child;
if (key < child->val)
{
child = child->left;
}
else
{
child = child->right;
}
}
if (child == nullptr) //说明树中不存在val==key的节点,我们直接return
{
return root;
}
if (child->left != nullptr && child->right != nullptr) //当该节点拥有两个child时
{
key = findMin(child->right); //我们转化成删除右子树中的最小值,因此原来的key值应该变为findMin的返回值
child->val = key;
parent = child;
child = child->right;
while (key != child->val)
{
parent = child;
if (key < child->val)
{
child = child->left;
}
else
{
child = child->right;
}
}
}
TreeNode* temp = child;
if (child->left != nullptr)
{
child = child->left;
}
else if (child->right != nullptr)
{
child = child->right;
}
else
{
child = nullptr;
}
delete temp;
if (key >= parent->val) //注意等于是为了处理有且仅有一个右节点的情况
{
parent->right = child;
}
else
{
parent->left = child;
}
return dummy->right;
}
};
1.2 findMax
class Solution
{
public:
int findMax(TreeNode* root)
{
while (root->right != nullptr)
{
root = root->right;
}
return root->val;
}
TreeNode* deleteNode(TreeNode* root, int key)
{
TreeNode* dummy = new TreeNode{ 1000000,root,root };
TreeNode* parent = dummy;
TreeNode* child = root;
if (root == nullptr)
{
return nullptr;
}
while (child != nullptr && key != child->val)
{
parent = child;
if (key < child->val)
{
child = child->left;
}
else
{
child = child->right;
}
}
if (child == nullptr)
{
return root;
}
if (child->left != nullptr && child->right != nullptr)
{
key = findMax(child->left);
child->val = key;
parent = child;
child = child->left;
while (key != child->val)
{
parent = child;
if (key < child->val)
{
child = child->left;
}
else
{
child = child->right;
}
}
}
TreeNode* temp = child;
if (child->left != nullptr)
{
child = child->left;
}
else if (child->right != nullptr)
{
child = child->right;
}
else
{
child = nullptr;
}
delete temp;
if (key <= parent->val)
{
parent->left = child;
}
else
{
parent->right = child;
}
return dummy->left;
}
};
方法二:递归
2.1 findMin
class Solution
{
public:
int findMin(TreeNode*root)
{
if(root->left!=nullptr)
{
return findMin(root->left);
}
return root->val;
}
TreeNode* deleteNode(TreeNode* root, int key)
{
if (root == nullptr)
{
return nullptr;
}
else if (key < root->val)
{
root->left = deleteNode(root->left, key);
}
else if (key > root->val)
{
root->right = deleteNode(root->right, key);
}
else
{
if (root->left != nullptr && root->right != nullptr)
{
root->val = findMin(root->right);
root->right=deleteNode(root->right, root->val);
}
else
{
TreeNode* temp = root;
if (root->left != nullptr)
{
root = root->left;
}
else if (root->right != nullptr)
{
root = root->right;
}
else
{
root = nullptr;
}
delete temp;
}
}
return root;
}
};
2.2 findMax
class Solution
{
public:
int findMax(TreeNode*root)
{
if(root->right!=nullptr)
{
return findMax(root->right);
}
return root->val;
}
TreeNode* deleteNode(TreeNode* root, int key)
{
if (root == nullptr)
{
return nullptr;
}
else if (key < root->val)
{
root->left = deleteNode(root->left, key);
}
else if (key > root->val)
{
root->right = deleteNode(root->right, key);
}
else
{
if (root->left != nullptr && root->right != nullptr)
{
root->val = findMax(root->left);
root->left=deleteNode(root->left, root->val);
}
else
{
TreeNode* temp = root;
if (root->left != nullptr)
{
root = root->left;
}
else if (root->right != nullptr)
{
root = root->right;
}
else
{
root = nullptr;
}
delete temp;
}
}
return root;
}
};