mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
Update 0654.最大二叉树.md
This commit is contained in:
@ -511,6 +511,35 @@ object Solution {
|
||||
|
||||
### Rust
|
||||
|
||||
新建数组:
|
||||
|
||||
```rust
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
impl Solution{
|
||||
pub fn construct_maximum_binary_tree(mut nums: Vec<i32>) -> Option<Rc<RefCell<TreeNode>>> {
|
||||
if nums.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let mut max_value_index = 0;
|
||||
for i in 0..nums.len() {
|
||||
if nums[max_value_index] < nums[i] {
|
||||
max_value_index = i;
|
||||
}
|
||||
}
|
||||
let right = Self::construct_maximum_binary_tree(nums.split_off(max_value_index + 1));
|
||||
let root = nums.pop().unwrap();
|
||||
let left = Self::construct_maximum_binary_tree(nums);
|
||||
Some(Rc::new(RefCell::new(TreeNode {
|
||||
val: root,
|
||||
left,
|
||||
right,
|
||||
})))
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
数组索引:
|
||||
```rust
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
Reference in New Issue
Block a user