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