From 4b575a8dab798fc213c56d21891e6d6e7446bbc5 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+fwqaaq@users.noreply.github.com> Date: Wed, 7 Dec 2022 23:18:23 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Update=200450.=E5=88=A0=E9=99=A4=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E8=8A=82?= =?UTF-8?q?=E7=82=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0450.删除二叉搜索树中的节点.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/problems/0450.删除二叉搜索树中的节点.md b/problems/0450.删除二叉搜索树中的节点.md index 0b4048d5..3d9a6050 100644 --- a/problems/0450.删除二叉搜索树中的节点.md +++ b/problems/0450.删除二叉搜索树中的节点.md @@ -657,6 +657,39 @@ object Solution { } ``` +## rust + +```rust +impl Solution { + pub fn delete_node( + root: Option>>, + key: i32, + ) -> Option>> { + root.as_ref()?; + + let mut node = root.as_ref().unwrap().borrow_mut(); + match node.val.cmp(&key) { + std::cmp::Ordering::Less => node.right = Self::delete_node(node.right.clone(), key), + std::cmp::Ordering::Equal => match (node.left.clone(), node.right.clone()) { + (None, None) => return None, + (None, Some(r)) => return Some(r), + (Some(l), None) => return Some(l), + (Some(l), Some(r)) => { + let mut cur = Some(r.clone()); + while let Some(n) = cur.clone().unwrap().borrow().left.clone() { + cur = Some(n); + } + cur.unwrap().borrow_mut().left = Some(l); + return Some(r); + } + }, + std::cmp::Ordering::Greater => node.left = Self::delete_node(node.left.clone(), key), + } + root.clone() + } +} +``` +

From d97f27621c96972c11b0e48b5498c411ac1e85f5 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+fwqaaq@users.noreply.github.com> Date: Fri, 9 Dec 2022 15:44:18 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Update=20problems/0450.=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E8=8A=82=E7=82=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0450.删除二叉搜索树中的节点.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/problems/0450.删除二叉搜索树中的节点.md b/problems/0450.删除二叉搜索树中的节点.md index 3d9a6050..424491b7 100644 --- a/problems/0450.删除二叉搜索树中的节点.md +++ b/problems/0450.删除二叉搜索树中的节点.md @@ -685,7 +685,8 @@ impl Solution { }, std::cmp::Ordering::Greater => node.left = Self::delete_node(node.left.clone(), key), } - root.clone() + drop(node); + root } } ```