Update 0108.将有序数组转换为二叉搜索树.md

This commit is contained in:
fw_qaq
2022-12-09 19:29:56 +08:00
committed by GitHub
parent 5af63a5ab5
commit 334c9a8b00

View File

@ -482,6 +482,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">