From 723286ddf8580c91c9af35bb38aca5c8440c11da Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+fwqaaq@users.noreply.github.com> Date: Sat, 17 Dec 2022 23:22:24 +0800 Subject: [PATCH] =?UTF-8?q?Update=200530.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E7=BB=9D=E5=AF=B9?= =?UTF-8?q?=E5=B7=AE.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0530.二叉搜索树的最小绝对差.md | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/problems/0530.二叉搜索树的最小绝对差.md b/problems/0530.二叉搜索树的最小绝对差.md index 8c47b633..bc9645dc 100644 --- a/problems/0530.二叉搜索树的最小绝对差.md +++ b/problems/0530.二叉搜索树的最小绝对差.md @@ -547,31 +547,25 @@ impl Solution { 递归中解决 ```rust -static mut PRE: Option = None; -static mut MIN: i32 = i32::MAX; - impl Solution { pub fn get_minimum_difference(root: Option>>) -> i32 { - unsafe { - PRE = None; - MIN = i32::MAX; - Self::inorder(root); - MIN - } + let mut pre = None; + let mut min = i32::MAX; + Self::inorder(root, &mut pre, &mut min); + min } - pub fn inorder(root: Option>>) { + pub fn inorder(root: Option>>, pre: &mut Option, min: &mut i32) { if root.is_none() { return; } let node = root.as_ref().unwrap().borrow(); - Self::inorder(node.left.clone()); - unsafe { - if let Some(pre) = PRE { - MIN = (node.val - pre).min(MIN).abs(); - } - PRE = Some(node.val) + Self::inorder(node.left.clone(), pre, min); + if let Some(pre) = pre { + *min = (node.val - *pre).min(*min); } - Self::inorder(node.right.clone()); + *pre = Some(node.val); + + Self::inorder(node.right.clone(), pre, min); } } ```