diff --git a/problems/0108.将有序数组转换为二叉搜索树.md b/problems/0108.将有序数组转换为二叉搜索树.md index 01c276fd..c75f87d1 100644 --- a/problems/0108.将有序数组转换为二叉搜索树.md +++ b/problems/0108.将有序数组转换为二叉搜索树.md @@ -486,6 +486,26 @@ object Solution { } ``` +## rust + +递归: + +```rust +impl Solution { + pub fn sorted_array_to_bst(nums: Vec) -> Option>> { + if nums.is_empty() { + return None; + } + let index = nums.len() / 2; + let mut root = TreeNode::new(nums[index]); + + root.left = Self::sorted_array_to_bst(nums[..index].to_vec()); + root.right = Self::sorted_array_to_bst(nums[index + 1..].to_vec()); + Some(Rc::new(RefCell::new(root))) + } +} +``` +