From 965afc9af3dc5c0fb8932937770f8395c98b7b86 Mon Sep 17 00:00:00 2001 From: life <13122192336@163.com> Date: Sat, 8 Jul 2023 17:20:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9java=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E8=A7=A3=E6=B3=95=E7=9A=84=E4=BD=8D=E7=BD=AE=EF=BC=8C=E9=80=82?= =?UTF-8?q?=E5=90=88=E5=9B=BE=E7=9A=84=E8=A7=A3=E6=B3=95=E6=94=BE=E5=9C=A8?= =?UTF-8?q?=E7=AC=AC=E4=B8=80=E4=B8=AA=EF=BC=8C=E7=AC=AC=E4=BA=8C=E4=B8=AA?= =?UTF-8?q?=E8=A7=A3=E6=B3=95=EF=BC=88=E9=9A=BE=E7=90=86=E8=A7=A3=E7=89=88?= =?UTF-8?q?=E6=9C=AC=EF=BC=89=E6=94=BE=E5=9C=A8=E5=85=B6=E5=90=8E=EF=BC=8C?= =?UTF-8?q?=E6=96=B0=E5=A2=9Ejava=E7=89=88=E6=9C=AC=E7=9A=84=E8=BF=AD?= =?UTF-8?q?=E4=BB=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0450.删除二叉搜索树中的节点.md | 86 +++++++++++++++---- 1 file changed, 69 insertions(+), 17 deletions(-) diff --git a/problems/0450.删除二叉搜索树中的节点.md b/problems/0450.删除二叉搜索树中的节点.md index 48737486..d13c48e5 100644 --- a/problems/0450.删除二叉搜索树中的节点.md +++ b/problems/0450.删除二叉搜索树中的节点.md @@ -268,6 +268,34 @@ public: ## Java +```java +// 解法1(最好理解的版本) +class Solution { + public TreeNode deleteNode(TreeNode root, int key) { + if (root == null) return root; + if (root.val == key) { + if (root.left == null) { + return root.right; + } else if (root.right == null) { + return root.left; + } else { + TreeNode cur = root.right; + while (cur.left != null) { + cur = cur.left; + } + cur.left = root.left; + root = root.right; + return root; + } + } + if (root.val > key) root.left = deleteNode(root.left, key); + if (root.val < key) root.right = deleteNode(root.right, key); + return root; + } +} +``` + + ```java class Solution { public TreeNode deleteNode(TreeNode root, int key) { @@ -296,33 +324,57 @@ class Solution { } } ``` +递归法 ```java -// 解法2 class Solution { public TreeNode deleteNode(TreeNode root, int key) { - if (root == null) return root; - if (root.val == key) { - if (root.left == null) { - return root.right; - } else if (root.right == null) { - return root.left; - } else { - TreeNode cur = root.right; - while (cur.left != null) { - cur = cur.left; - } - cur.left = root.left; - root = root.right; - return root; + if (root == null){ + return null; + } + //寻找对应的对应的前面的节点,以及他的前一个节点 + TreeNode cur = root; + TreeNode pre = null; + while (cur != null){ + if (cur.val < key){ + pre = cur; + cur = cur.right; + } else if (cur.val > key) { + pre = cur; + cur = cur.left; + }else { + break; } } - if (root.val > key) root.left = deleteNode(root.left, key); - if (root.val < key) root.right = deleteNode(root.right, key); + if (pre == null){ + return deleteOneNode(cur); + } + if (pre.left !=null && pre.left.val == key){ + pre.left = deleteOneNode(cur); + } + if (pre.right !=null && pre.right.val == key){ + pre.right = deleteOneNode(cur); + } return root; } + + public TreeNode deleteOneNode(TreeNode node){ + if (node == null){ + return null; + } + if (node.right == null){ + return node.left; + } + TreeNode cur = node.right; + while (cur.left !=null){ + cur = cur.left; + } + cur.left = node.left; + return node.right; + } } ``` + ## Python 递归法(版本一) ```python