Merge pull request #1811 from fwqaaq/patch-12

Update 0669.修剪二叉搜索树.md about rust
This commit is contained in:
程序员Carl
2022-12-28 12:00:11 +08:00
committed by GitHub

View File

@ -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">