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

This commit is contained in:
fw_qaq
2022-11-28 23:47:47 +08:00
committed by GitHub
parent d610b113e8
commit 00f080bc75

View File

@ -515,6 +515,35 @@ object Solution {
}
```
## rust
构建二叉树的有序数组:
```rust
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
pub fn get_minimum_difference(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
let mut vec = vec![];
Self::traversal(root, &mut vec);
let mut min = i32::MAX;
for i in 1..vec.len() {
min = min.min(vec[i] - vec[i - 1])
}
min
}
pub fn traversal(root: Option<Rc<RefCell<TreeNode>>>, v: &mut Vec<i32>) {
if root.is_none() {
return;
}
let node = root.as_ref().unwrap().borrow();
Self::traversal(node.left.clone(), v);
v.push(node.val);
Self::traversal(node.right.clone(), v);
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>