mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
null写成了nullptr
This commit is contained in:
@ -85,14 +85,14 @@ if (root == null) return root;
|
|||||||
if (root->val == key) {
|
if (root->val == key) {
|
||||||
// 第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点
|
// 第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点
|
||||||
// 第三种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点
|
// 第三种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点
|
||||||
if (root->left == nullptr) return root->right;
|
if (root->left == null) return root->right;
|
||||||
// 第四种情况:其右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点
|
// 第四种情况:其右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点
|
||||||
else if (root->right == nullptr) return root->left;
|
else if (root->right == null) return root->left;
|
||||||
// 第五种情况:左右孩子节点都不为空,则将删除节点的左子树放到删除节点的右子树的最左面节点的左孩子的位置
|
// 第五种情况:左右孩子节点都不为空,则将删除节点的左子树放到删除节点的右子树的最左面节点的左孩子的位置
|
||||||
// 并返回删除节点右孩子为新的根节点。
|
// 并返回删除节点右孩子为新的根节点。
|
||||||
else {
|
else {
|
||||||
TreeNode* cur = root->right; // 找右子树最左面的节点
|
TreeNode* cur = root->right; // 找右子树最左面的节点
|
||||||
while(cur->left != nullptr) {
|
while(cur->left != null) {
|
||||||
cur = cur->left;
|
cur = cur->left;
|
||||||
}
|
}
|
||||||
cur->left = root->left; // 把要删除的节点(root)左子树放在cur的左孩子的位置
|
cur->left = root->left; // 把要删除的节点(root)左子树放在cur的左孩子的位置
|
||||||
@ -118,23 +118,23 @@ return root;
|
|||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
TreeNode* deleteNode(TreeNode* root, int key) {
|
TreeNode* deleteNode(TreeNode* root, int key) {
|
||||||
if (root == nullptr) return root; // 第一种情况:没找到删除的节点,遍历到空节点直接返回了
|
if (root == null) return root; // 第一种情况:没找到删除的节点,遍历到空节点直接返回了
|
||||||
if (root->val == key) {
|
if (root->val == key) {
|
||||||
// 第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点
|
// 第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点
|
||||||
if (root->left == nullptr && root->right == nullptr) {
|
if (root->left == null && root->right == null) {
|
||||||
///! 内存释放
|
///! 内存释放
|
||||||
delete root;
|
delete root;
|
||||||
return nullptr;
|
return null;
|
||||||
}
|
}
|
||||||
// 第三种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点
|
// 第三种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点
|
||||||
else if (root->left == nullptr) {
|
else if (root->left == null) {
|
||||||
auto retNode = root->right;
|
auto retNode = root->right;
|
||||||
///! 内存释放
|
///! 内存释放
|
||||||
delete root;
|
delete root;
|
||||||
return retNode;
|
return retNode;
|
||||||
}
|
}
|
||||||
// 第四种情况:其右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点
|
// 第四种情况:其右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点
|
||||||
else if (root->right == nullptr) {
|
else if (root->right == null) {
|
||||||
auto retNode = root->left;
|
auto retNode = root->left;
|
||||||
///! 内存释放
|
///! 内存释放
|
||||||
delete root;
|
delete root;
|
||||||
@ -144,7 +144,7 @@ public:
|
|||||||
// 并返回删除节点右孩子为新的根节点。
|
// 并返回删除节点右孩子为新的根节点。
|
||||||
else {
|
else {
|
||||||
TreeNode* cur = root->right; // 找右子树最左面的节点
|
TreeNode* cur = root->right; // 找右子树最左面的节点
|
||||||
while(cur->left != nullptr) {
|
while(cur->left != null) {
|
||||||
cur = cur->left;
|
cur = cur->left;
|
||||||
}
|
}
|
||||||
cur->left = root->left; // 把要删除的节点(root)左子树放在cur的左孩子的位置
|
cur->left = root->left; // 把要删除的节点(root)左子树放在cur的左孩子的位置
|
||||||
@ -178,9 +178,9 @@ public:
|
|||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
TreeNode* deleteNode(TreeNode* root, int key) {
|
TreeNode* deleteNode(TreeNode* root, int key) {
|
||||||
if (root == nullptr) return root;
|
if (root == null) return root;
|
||||||
if (root->val == key) {
|
if (root->val == key) {
|
||||||
if (root->right == nullptr) { // 这里第二次操作目标值:最终删除的作用
|
if (root->right == null) { // 这里第二次操作目标值:最终删除的作用
|
||||||
return root->left;
|
return root->left;
|
||||||
}
|
}
|
||||||
TreeNode *cur = root->right;
|
TreeNode *cur = root->right;
|
||||||
@ -211,8 +211,8 @@ private:
|
|||||||
// 并返回目标节点右孩子为新的根节点
|
// 并返回目标节点右孩子为新的根节点
|
||||||
// 是动画里模拟的过程
|
// 是动画里模拟的过程
|
||||||
TreeNode* deleteOneNode(TreeNode* target) {
|
TreeNode* deleteOneNode(TreeNode* target) {
|
||||||
if (target == nullptr) return target;
|
if (target == null) return target;
|
||||||
if (target->right == nullptr) return target->left;
|
if (target->right == null) return target->left;
|
||||||
TreeNode* cur = target->right;
|
TreeNode* cur = target->right;
|
||||||
while (cur->left) {
|
while (cur->left) {
|
||||||
cur = cur->left;
|
cur = cur->left;
|
||||||
@ -222,16 +222,16 @@ private:
|
|||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
TreeNode* deleteNode(TreeNode* root, int key) {
|
TreeNode* deleteNode(TreeNode* root, int key) {
|
||||||
if (root == nullptr) return root;
|
if (root == null) return root;
|
||||||
TreeNode* cur = root;
|
TreeNode* cur = root;
|
||||||
TreeNode* pre = nullptr; // 记录cur的父节点,用来删除cur
|
TreeNode* pre = null; // 记录cur的父节点,用来删除cur
|
||||||
while (cur) {
|
while (cur) {
|
||||||
if (cur->val == key) break;
|
if (cur->val == key) break;
|
||||||
pre = cur;
|
pre = cur;
|
||||||
if (cur->val > key) cur = cur->left;
|
if (cur->val > key) cur = cur->left;
|
||||||
else cur = cur->right;
|
else cur = cur->right;
|
||||||
}
|
}
|
||||||
if (pre == nullptr) { // 如果搜索树只有头结点
|
if (pre == null) { // 如果搜索树只有头结点
|
||||||
return deleteOneNode(cur);
|
return deleteOneNode(cur);
|
||||||
}
|
}
|
||||||
// pre 要知道是删左孩子还是右孩子
|
// pre 要知道是删左孩子还是右孩子
|
||||||
|
Reference in New Issue
Block a user