Update 0108.将有序数组转换为二叉搜索树.md
This commit is contained in:
parent
5af63a5ab5
commit
334c9a8b00
|
|
@ -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">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue