From 0fd7b04d5c0fac9a760af96d2d5de7f5beaeb896 Mon Sep 17 00:00:00 2001 From: life <13122192336@163.com> Date: Sat, 8 Jul 2023 17:42:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=92=A4=E5=9B=9E=E4=B9=8B=E5=89=8D=E7=9A=84nu?= =?UTF-8?q?llptr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0450.删除二叉搜索树中的节点.md | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/problems/0450.删除二叉搜索树中的节点.md b/problems/0450.删除二叉搜索树中的节点.md index d13c48e5..e617cc69 100644 --- a/problems/0450.删除二叉搜索树中的节点.md +++ b/problems/0450.删除二叉搜索树中的节点.md @@ -51,7 +51,7 @@ TreeNode* deleteNode(TreeNode* root, int key) 遇到空返回,其实这也说明没找到删除的节点,遍历到空节点直接返回了 ``` -if (root == null) return root; +if (root == nullptr) return root; ``` * 确定单层递归的逻辑 @@ -85,14 +85,14 @@ if (root == null) return root; if (root->val == key) { // 第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点 // 第三种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点 - if (root->left == null) return root->right; + if (root->left == nullptr) return root->right; // 第四种情况:其右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点 - else if (root->right == null) return root->left; + else if (root->right == nullptr) return root->left; // 第五种情况:左右孩子节点都不为空,则将删除节点的左子树放到删除节点的右子树的最左面节点的左孩子的位置 // 并返回删除节点右孩子为新的根节点。 else { TreeNode* cur = root->right; // 找右子树最左面的节点 - while(cur->left != null) { + while(cur->left != nullptr) { 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 == null) return root; // 第一种情况:没找到删除的节点,遍历到空节点直接返回了 + if (root == nullptr) return root; // 第一种情况:没找到删除的节点,遍历到空节点直接返回了 if (root->val == key) { // 第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点 - if (root->left == null && root->right == null) { + if (root->left == nullptr && root->right == nullptr) { ///! 内存释放 delete root; - return null; + return nullptr; } // 第三种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点 - else if (root->left == null) { + else if (root->left == nullptr) { auto retNode = root->right; ///! 内存释放 delete root; return retNode; } // 第四种情况:其右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点 - else if (root->right == null) { + else if (root->right == nullptr) { auto retNode = root->left; ///! 内存释放 delete root; @@ -144,7 +144,7 @@ public: // 并返回删除节点右孩子为新的根节点。 else { TreeNode* cur = root->right; // 找右子树最左面的节点 - while(cur->left != null) { + while(cur->left != nullptr) { 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 == null) return root; + if (root == nullptr) return root; if (root->val == key) { - if (root->right == null) { // 这里第二次操作目标值:最终删除的作用 + if (root->right == nullptr) { // 这里第二次操作目标值:最终删除的作用 return root->left; } TreeNode *cur = root->right; @@ -211,8 +211,8 @@ private: // 并返回目标节点右孩子为新的根节点 // 是动画里模拟的过程 TreeNode* deleteOneNode(TreeNode* target) { - if (target == null) return target; - if (target->right == null) return target->left; + if (target == nullptr) return target; + if (target->right == nullptr) 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 == null) return root; + if (root == nullptr) return root; TreeNode* cur = root; - TreeNode* pre = null; // 记录cur的父节点,用来删除cur + TreeNode* pre = nullptr; // 记录cur的父节点,用来删除cur while (cur) { if (cur->val == key) break; pre = cur; if (cur->val > key) cur = cur->left; else cur = cur->right; } - if (pre == null) { // 如果搜索树只有头结点 + if (pre == nullptr) { // 如果搜索树只有头结点 return deleteOneNode(cur); } // pre 要知道是删左孩子还是右孩子