mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
Merge pull request #1812 from fwqaaq/patch-15
Update 0108.将有序数组转换为二叉搜索树.md about rust
This commit is contained in:
@ -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">
|
||||
|
Reference in New Issue
Block a user