File tree 1 file changed +36
-1
lines changed 1 file changed +36
-1
lines changed Original file line number Diff line number Diff line change @@ -28,4 +28,39 @@ public:
28
28
};
29
29
30
30
```
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
+ ```
You can’t perform that action at this time.
0 commit comments