mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Update 0530.二叉搜索树的最小绝对差.md
This commit is contained in:
@ -576,6 +576,38 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
|
||||
迭代
|
||||
|
||||
```rust
|
||||
impl Solution {
|
||||
pub fn get_minimum_difference(mut root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
|
||||
if root.is_none() {
|
||||
return 0;
|
||||
}
|
||||
let mut stack = vec![];
|
||||
let mut pre = -1;
|
||||
let mut res = i32::MAX;
|
||||
while root.is_some() || !stack.is_empty() {
|
||||
while let Some(node) = root {
|
||||
root = node.borrow().left.clone();
|
||||
stack.push(node);
|
||||
}
|
||||
|
||||
let node = stack.pop().unwrap();
|
||||
|
||||
if pre >= 0 {
|
||||
res = res.min(node.borrow().val - pre);
|
||||
}
|
||||
|
||||
pre = node.borrow().val;
|
||||
|
||||
root = node.borrow().right.clone();
|
||||
}
|
||||
res
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
|
Reference in New Issue
Block a user