diff --git a/problems/0669.修剪二叉搜索树.md b/problems/0669.修剪二叉搜索树.md index 1c5fdea1..ac36509f 100644 --- a/problems/0669.修剪二叉搜索树.md +++ b/problems/0669.修剪二叉搜索树.md @@ -473,6 +473,33 @@ object Solution { } ``` +## rust + +// 递归 +```rust +impl Solution { + pub fn trim_bst( + root: Option>>, + low: i32, + high: i32, + ) -> Option>> { + 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 + } +} +``` +