-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathlib.rs
73 lines (72 loc) · 1.81 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* @lc app=leetcode.cn id=654 lang=rust
*
* [654] 最大二叉树
*/
// #[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
pub val: i32,
pub left: Option<Rc<RefCell<TreeNode>>>,
pub right: Option<Rc<RefCell<TreeNode>>>,
}
impl TreeNode {
#[inline]
pub fn new(val: i32) -> Self {
TreeNode {
val,
left: None,
right: None,
}
}
}
struct Solution {}
// @lc code=start
// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
// pub val: i32,
// pub left: Option<Rc<RefCell<TreeNode>>>,
// pub right: Option<Rc<RefCell<TreeNode>>>,
// }
//
// impl TreeNode {
// #[inline]
// pub fn new(val: i32) -> Self {
// TreeNode {
// val,
// left: None,
// right: None
// }
// }
// }
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
pub fn construct_maximum_binary_tree(nums: Vec<i32>) -> Option<Rc<RefCell<TreeNode>>> {
if nums.len() == 0 {
return None;
}
let index = Self::get_max(&nums);
let mut root = Self::create_node(nums[index]);
let nums_left = nums[0..index].to_vec();
let nums_right = nums[index + 1..nums.len()].to_vec();
root.as_mut()?.borrow_mut().left = Self::construct_maximum_binary_tree(nums_left);
root.as_mut()?.borrow_mut().right = Self::construct_maximum_binary_tree(nums_right);
root
}
fn get_max(v: &Vec<i32>) -> usize {
let mut index = 0;
let mut MAX = v[index];
for i in 0..v.len() {
if v[i] > MAX {
MAX = v[i];
index = i
}
}
index
}
fn create_node(val: i32) -> Option<Rc<RefCell<TreeNode>>> {
Some(Rc::new(RefCell::new(TreeNode::new(val))))
}
}
// @lc code=end