From e60af48906349690cb23ee74cd1584674de66299 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Tue, 29 Nov 2022 01:00:18 +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 | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0530.二叉搜索树的最小绝对差.md b/problems/0530.二叉搜索树的最小绝对差.md index d1c1e1d4..984fefe5 100644 --- a/problems/0530.二叉搜索树的最小绝对差.md +++ b/problems/0530.二叉搜索树的最小绝对差.md @@ -544,6 +544,38 @@ 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 + } + } + pub fn inorder(root: Option>>) { + 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.right.clone()); + } +} +``` +