Skip to content

Commit 55d773f

Browse files
authored
feat: add rust solution to lc problem: No.0530 (doocs#1331)
1 parent 757f45f commit 55d773f

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed

solution/0500-0599/0530.Minimum Absolute Difference in BST/README.md

+49
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,55 @@ public:
156156
};
157157
```
158158
159+
### **Rust**
160+
161+
```rust
162+
// Definition for a binary tree node.
163+
// #[derive(Debug, PartialEq, Eq)]
164+
// pub struct TreeNode {
165+
// pub val: i32,
166+
// pub left: Option<Rc<RefCell<TreeNode>>>,
167+
// pub right: Option<Rc<RefCell<TreeNode>>>,
168+
// }
169+
//
170+
// impl TreeNode {
171+
// #[inline]
172+
// pub fn new(val: i32) -> Self {
173+
// TreeNode {
174+
// val,
175+
// left: None,
176+
// right: None
177+
// }
178+
// }
179+
// }
180+
use std::rc::Rc;
181+
use std::cell::RefCell;
182+
impl Solution {
183+
#[allow(dead_code)]
184+
pub fn get_minimum_difference(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
185+
let mut ret = i32::MAX;
186+
let mut prev = i32::MAX;
187+
Self::traverse(root, &mut prev, &mut ret);
188+
ret
189+
}
190+
191+
#[allow(dead_code)]
192+
fn traverse(root: Option<Rc<RefCell<TreeNode>>>, prev: &mut i32, ans: &mut i32) {
193+
let left = root.as_ref().unwrap().borrow().left.clone();
194+
let right = root.as_ref().unwrap().borrow().right.clone();
195+
let val = root.as_ref().unwrap().borrow().val;
196+
if !left.is_none() {
197+
Self::traverse(left.clone(), prev, ans);
198+
}
199+
*ans = std::cmp::min(*ans, (*prev - val).abs());
200+
*prev = val;
201+
if !right.is_none() {
202+
Self::traverse(right.clone(), prev, ans);
203+
}
204+
}
205+
}
206+
```
207+
159208
### **Go**
160209

161210
```go

solution/0500-0599/0530.Minimum Absolute Difference in BST/README_EN.md

+49
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,55 @@ public:
139139
};
140140
```
141141
142+
### **Rust**
143+
144+
```rust
145+
// Definition for a binary tree node.
146+
// #[derive(Debug, PartialEq, Eq)]
147+
// pub struct TreeNode {
148+
// pub val: i32,
149+
// pub left: Option<Rc<RefCell<TreeNode>>>,
150+
// pub right: Option<Rc<RefCell<TreeNode>>>,
151+
// }
152+
//
153+
// impl TreeNode {
154+
// #[inline]
155+
// pub fn new(val: i32) -> Self {
156+
// TreeNode {
157+
// val,
158+
// left: None,
159+
// right: None
160+
// }
161+
// }
162+
// }
163+
use std::rc::Rc;
164+
use std::cell::RefCell;
165+
impl Solution {
166+
#[allow(dead_code)]
167+
pub fn get_minimum_difference(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
168+
let mut ret = i32::MAX;
169+
let mut prev = i32::MAX;
170+
Self::traverse(root, &mut prev, &mut ret);
171+
ret
172+
}
173+
174+
#[allow(dead_code)]
175+
fn traverse(root: Option<Rc<RefCell<TreeNode>>>, prev: &mut i32, ans: &mut i32) {
176+
let left = root.as_ref().unwrap().borrow().left.clone();
177+
let right = root.as_ref().unwrap().borrow().right.clone();
178+
let val = root.as_ref().unwrap().borrow().val;
179+
if !left.is_none() {
180+
Self::traverse(left.clone(), prev, ans);
181+
}
182+
*ans = std::cmp::min(*ans, (*prev - val).abs());
183+
*prev = val;
184+
if !right.is_none() {
185+
Self::traverse(right.clone(), prev, ans);
186+
}
187+
}
188+
}
189+
```
190+
142191
### **Go**
143192

144193
```go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Definition for a binary tree node.
2+
// #[derive(Debug, PartialEq, Eq)]
3+
// pub struct TreeNode {
4+
// pub val: i32,
5+
// pub left: Option<Rc<RefCell<TreeNode>>>,
6+
// pub right: Option<Rc<RefCell<TreeNode>>>,
7+
// }
8+
//
9+
// impl TreeNode {
10+
// #[inline]
11+
// pub fn new(val: i32) -> Self {
12+
// TreeNode {
13+
// val,
14+
// left: None,
15+
// right: None
16+
// }
17+
// }
18+
// }
19+
use std::rc::Rc;
20+
use std::cell::RefCell;
21+
impl Solution {
22+
#[allow(dead_code)]
23+
pub fn get_minimum_difference(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
24+
let mut ret = i32::MAX;
25+
let mut prev = i32::MAX;
26+
Self::traverse(root, &mut prev, &mut ret);
27+
ret
28+
}
29+
30+
#[allow(dead_code)]
31+
fn traverse(root: Option<Rc<RefCell<TreeNode>>>, prev: &mut i32, ans: &mut i32) {
32+
let left = root.as_ref().unwrap().borrow().left.clone();
33+
let right = root.as_ref().unwrap().borrow().right.clone();
34+
let val = root.as_ref().unwrap().borrow().val;
35+
if !left.is_none() {
36+
Self::traverse(left.clone(), prev, ans);
37+
}
38+
*ans = std::cmp::min(*ans, (*prev - val).abs());
39+
*prev = val;
40+
if !right.is_none() {
41+
Self::traverse(right.clone(), prev, ans);
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)