mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
Merge pull request #1811 from fwqaaq/patch-12
Update 0669.修剪二叉搜索树.md about rust
This commit is contained in:
@ -473,6 +473,33 @@ object Solution {
|
||||
}
|
||||
```
|
||||
|
||||
## rust
|
||||
|
||||
// 递归
|
||||
```rust
|
||||
impl Solution {
|
||||
pub fn trim_bst(
|
||||
root: Option<Rc<RefCell<TreeNode>>>,
|
||||
low: i32,
|
||||
high: i32,
|
||||
) -> Option<Rc<RefCell<TreeNode>>> {
|
||||
root.as_ref()?;
|
||||
let mut node = root.as_ref().unwrap().borrow_mut();
|
||||
if node.val < low {
|
||||
return Self::trim_bst(node.right.clone(), low, high);
|
||||
}
|
||||
if node.val > high {
|
||||
return Self::trim_bst(node.left.clone(), low, high);
|
||||
}
|
||||
|
||||
node.left = Self::trim_bst(node.left.clone(), low, high);
|
||||
node.right = Self::trim_bst(node.right.clone(), low, high);
|
||||
drop(node);
|
||||
root
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
|
Reference in New Issue
Block a user