mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Update 0530.二叉搜索树的最小绝对差.md
This commit is contained in:
@ -544,6 +544,38 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
递归中解决
|
||||||
|
|
||||||
|
```rust
|
||||||
|
static mut PRE: Option<i32> = None;
|
||||||
|
static mut MIN: i32 = i32::MAX;
|
||||||
|
|
||||||
|
impl Solution {
|
||||||
|
pub fn get_minimum_difference(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
|
||||||
|
unsafe {
|
||||||
|
PRE = None;
|
||||||
|
MIN = i32::MAX;
|
||||||
|
Self::inorder(root);
|
||||||
|
MIN
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn inorder(root: Option<Rc<RefCell<TreeNode>>>) {
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
|
Reference in New Issue
Block a user