-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathSolution.rs
79 lines (73 loc) · 2.09 KB
/
Solution.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
74
75
76
77
78
79
// 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
// }
// }
// }
impl TreeNode {
pub fn new_with_node(
left: Option<Rc<RefCell<TreeNode>>>,
right: Option<Rc<RefCell<TreeNode>>>
) -> Self {
Self {
val: 0,
left,
right,
}
}
}
use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
#[allow(dead_code)]
pub fn all_possible_fbt(n: i32) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
let mut record_vec = vec![vec![]; n as usize + 1];
Self::dfs(n, &mut record_vec)
}
#[allow(dead_code)]
fn dfs(
n: i32,
record_vec: &mut Vec<Vec<Option<Rc<RefCell<TreeNode>>>>>
) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
if record_vec[n as usize].len() != 0 {
return record_vec[n as usize].clone();
}
if n == 1 {
// Just directly return a single node
return vec![Some(Rc::new(RefCell::new(TreeNode::new(0))))];
}
// Otherwise, need to construct return vector
let mut ret_vec = Vec::new();
// Enumerate the node number for left subtree from 0 -> n - 1
for i in 0..n - 1 {
// The number of right subtree node
let j = n - i - 1;
for left in Self::dfs(i, record_vec) {
for right in Self::dfs(j, record_vec) {
// Construct the ret vector
ret_vec.push(
Some(
Rc::new(
RefCell::new(TreeNode::new_with_node(left.clone(), right.clone()))
)
)
);
}
}
}
record_vec[n as usize] = ret_vec;
record_vec[n as usize].clone()
}
}