Skip to content

Commit 0202122

Browse files
committed
1
1 parent 257ced0 commit 0202122

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

notes/src/day20/lc654.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,39 @@ public:
2828
};
2929

3030
```
31-
## 学习感想
31+
## 学习感想
32+
33+
```rust
34+
35+
// Definition for a binary tree node.
36+
#[derive(Debug, PartialEq, Eq)]
37+
pub struct TreeNode {
38+
pub val: i32,
39+
pub left: Option<Rc<RefCell<TreeNode>>>,
40+
pub right: Option<Rc<RefCell<TreeNode>>>,
41+
}
42+
43+
impl TreeNode {
44+
#[inline]
45+
pub fn new(val: i32) -> Self {
46+
TreeNode {
47+
val,
48+
left: None,
49+
right: None
50+
}
51+
}
52+
}
53+
use std::rc::Rc;
54+
use std::cell::RefCell;
55+
struct Solution {}
56+
impl Solution {
57+
pub fn construct_maximum_binary_tree(nums: Vec<i32>) -> Option<Rc<RefCell<TreeNode>>> {
58+
if nums.len() == 0 { return None }
59+
let (pos, &num): (usize, &i32) = nums.iter().enumerate().max_by_key(|(_, &x)| x).unwrap();
60+
let mut tn: TreeNode = TreeNode::new(num);
61+
tn.left = Self::construct_maximum_binary_tree(nums[.. pos].to_owned());
62+
tn.right = Self::construct_maximum_binary_tree(nums[pos + 1usize .. ].to_owned());
63+
Some(Rc::new(RefCell::new(tn)))
64+
}
65+
}
66+
```

0 commit comments

Comments
 (0)