mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
Update 0530.二叉搜索树的最小绝对差.md
This commit is contained in:
@ -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"/>
|
||||
|
Reference in New Issue
Block a user