Skip to content

Commit f06d81c

Browse files
authored
Update 0530.二叉搜索树的最小绝对差.md
1 parent e60af48 commit f06d81c

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,38 @@ impl Solution {
576576
}
577577
```
578578

579+
迭代
580+
581+
```rust
582+
impl Solution {
583+
pub fn get_minimum_difference(mut root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
584+
if root.is_none() {
585+
return 0;
586+
}
587+
let mut stack = vec![];
588+
let mut pre = -1;
589+
let mut res = i32::MAX;
590+
while root.is_some() || !stack.is_empty() {
591+
while let Some(node) = root {
592+
root = node.borrow().left.clone();
593+
stack.push(node);
594+
}
595+
596+
let node = stack.pop().unwrap();
597+
598+
if pre >= 0 {
599+
res = res.min(node.borrow().val - pre);
600+
}
601+
602+
pre = node.borrow().val;
603+
604+
root = node.borrow().right.clone();
605+
}
606+
res
607+
}
608+
}
609+
```
610+
579611
<p align="center">
580612
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
581613
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

0 commit comments

Comments
 (0)