Update 0530.二叉搜索树的最小绝对差.md

This commit is contained in:
fw_qaq
2022-11-29 12:48:10 +08:00
committed by GitHub
parent e60af48906
commit f06d81c233

View File

@ -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"> <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"/>