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()); + } +} +``` +