Merge pull request #1812 from fwqaaq/patch-15

Update 0108.将有序数组转换为二叉搜索树.md about rust
This commit is contained in:
程序员Carl
2022-12-28 12:00:27 +08:00
committed by GitHub

View File

@ -486,6 +486,26 @@ object Solution {
}
```
## rust
递归:
```rust
impl Solution {
pub fn sorted_array_to_bst(nums: Vec<i32>) -> Option<Rc<RefCell<TreeNode>>> {
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)))
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">