Skip to content

Commit 88a250f

Browse files
authored
feat: add rust solution to lc problem: No.0894 (doocs#1274)
1 parent 73b1161 commit 88a250f

File tree

3 files changed

+211
-0
lines changed

3 files changed

+211
-0
lines changed

solution/0800-0899/0894.All Possible Full Binary Trees/README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,78 @@ public:
170170
};
171171
```
172172
173+
### **Rust**
174+
175+
```rust
176+
// Definition for a binary tree node.
177+
// #[derive(Debug, PartialEq, Eq)]
178+
// pub struct TreeNode {
179+
// pub val: i32,
180+
// pub left: Option<Rc<RefCell<TreeNode>>>,
181+
// pub right: Option<Rc<RefCell<TreeNode>>>,
182+
// }
183+
//
184+
// impl TreeNode {
185+
// #[inline]
186+
// pub fn new(val: i32) -> Self {
187+
// TreeNode {
188+
// val,
189+
// left: None,
190+
// right: None
191+
// }
192+
// }
193+
// }
194+
195+
impl TreeNode {
196+
pub fn new_with_node(left: Option<Rc<RefCell<TreeNode>>>, right: Option<Rc<RefCell<TreeNode>>>) -> Self {
197+
Self {
198+
val: 0,
199+
left,
200+
right,
201+
}
202+
}
203+
}
204+
205+
use std::rc::Rc;
206+
use std::cell::RefCell;
207+
impl Solution {
208+
#[allow(dead_code)]
209+
pub fn all_possible_fbt(n: i32) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
210+
let mut record_vec = vec![vec![]; n as usize + 1];
211+
Self::dfs(n, &mut record_vec)
212+
}
213+
214+
#[allow(dead_code)]
215+
fn dfs(n: i32, record_vec: &mut Vec<Vec<Option<Rc<RefCell<TreeNode>>>>>) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
216+
if record_vec[n as usize].len() != 0 {
217+
return record_vec[n as usize].clone();
218+
}
219+
if n == 1 {
220+
// Just directly return a single node
221+
return vec![Some(Rc::new(RefCell::new(TreeNode::new(0))))];
222+
}
223+
// Otherwise, need to construct return vector
224+
let mut ret_vec = Vec::new();
225+
226+
// Enumerate the node number for left subtree from 0 -> n - 1
227+
for i in 0..n - 1 {
228+
// The number of right subtree node
229+
let j = n - i - 1;
230+
for left in Self::dfs(i, record_vec) {
231+
for right in Self::dfs(j, record_vec) {
232+
// Construct the ret vector
233+
ret_vec.push(Some(Rc::new(RefCell::new(TreeNode::new_with_node(left.clone(), right.clone())))));
234+
}
235+
}
236+
}
237+
238+
record_vec[n as usize] = ret_vec;
239+
240+
record_vec[n as usize].clone()
241+
}
242+
}
243+
```
244+
173245
### **Go**
174246

175247
```go

solution/0800-0899/0894.All Possible Full Binary Trees/README_EN.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,78 @@ public:
150150
};
151151
```
152152
153+
### **Rust**
154+
155+
```rust
156+
// Definition for a binary tree node.
157+
// #[derive(Debug, PartialEq, Eq)]
158+
// pub struct TreeNode {
159+
// pub val: i32,
160+
// pub left: Option<Rc<RefCell<TreeNode>>>,
161+
// pub right: Option<Rc<RefCell<TreeNode>>>,
162+
// }
163+
//
164+
// impl TreeNode {
165+
// #[inline]
166+
// pub fn new(val: i32) -> Self {
167+
// TreeNode {
168+
// val,
169+
// left: None,
170+
// right: None
171+
// }
172+
// }
173+
// }
174+
175+
impl TreeNode {
176+
pub fn new_with_node(left: Option<Rc<RefCell<TreeNode>>>, right: Option<Rc<RefCell<TreeNode>>>) -> Self {
177+
Self {
178+
val: 0,
179+
left,
180+
right,
181+
}
182+
}
183+
}
184+
185+
use std::rc::Rc;
186+
use std::cell::RefCell;
187+
impl Solution {
188+
#[allow(dead_code)]
189+
pub fn all_possible_fbt(n: i32) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
190+
let mut record_vec = vec![vec![]; n as usize + 1];
191+
Self::dfs(n, &mut record_vec)
192+
}
193+
194+
#[allow(dead_code)]
195+
fn dfs(n: i32, record_vec: &mut Vec<Vec<Option<Rc<RefCell<TreeNode>>>>>) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
196+
if record_vec[n as usize].len() != 0 {
197+
return record_vec[n as usize].clone();
198+
}
199+
if n == 1 {
200+
// Just directly return a single node
201+
return vec![Some(Rc::new(RefCell::new(TreeNode::new(0))))];
202+
}
203+
// Otherwise, need to construct return vector
204+
let mut ret_vec = Vec::new();
205+
206+
// Enumerate the node number for left subtree from 0 -> n - 1
207+
for i in 0..n - 1 {
208+
// The number of right subtree node
209+
let j = n - i - 1;
210+
for left in Self::dfs(i, record_vec) {
211+
for right in Self::dfs(j, record_vec) {
212+
// Construct the ret vector
213+
ret_vec.push(Some(Rc::new(RefCell::new(TreeNode::new_with_node(left.clone(), right.clone())))));
214+
}
215+
}
216+
}
217+
218+
record_vec[n as usize] = ret_vec;
219+
220+
record_vec[n as usize].clone()
221+
}
222+
}
223+
```
224+
153225
### **Go**
154226

155227
```go
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Definition for a binary tree node.
2+
// #[derive(Debug, PartialEq, Eq)]
3+
// pub struct TreeNode {
4+
// pub val: i32,
5+
// pub left: Option<Rc<RefCell<TreeNode>>>,
6+
// pub right: Option<Rc<RefCell<TreeNode>>>,
7+
// }
8+
//
9+
// impl TreeNode {
10+
// #[inline]
11+
// pub fn new(val: i32) -> Self {
12+
// TreeNode {
13+
// val,
14+
// left: None,
15+
// right: None
16+
// }
17+
// }
18+
// }
19+
20+
impl TreeNode {
21+
pub fn new_with_node(left: Option<Rc<RefCell<TreeNode>>>, right: Option<Rc<RefCell<TreeNode>>>) -> Self {
22+
Self {
23+
val: 0,
24+
left,
25+
right,
26+
}
27+
}
28+
}
29+
30+
use std::rc::Rc;
31+
use std::cell::RefCell;
32+
impl Solution {
33+
#[allow(dead_code)]
34+
pub fn all_possible_fbt(n: i32) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
35+
let mut record_vec = vec![vec![]; n as usize + 1];
36+
Self::dfs(n, &mut record_vec)
37+
}
38+
39+
#[allow(dead_code)]
40+
fn dfs(n: i32, record_vec: &mut Vec<Vec<Option<Rc<RefCell<TreeNode>>>>>) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
41+
if record_vec[n as usize].len() != 0 {
42+
return record_vec[n as usize].clone();
43+
}
44+
if n == 1 {
45+
// Just directly return a single node
46+
return vec![Some(Rc::new(RefCell::new(TreeNode::new(0))))];
47+
}
48+
// Otherwise, need to construct return vector
49+
let mut ret_vec = Vec::new();
50+
51+
// Enumerate the node number for left subtree from 0 -> n - 1
52+
for i in 0..n - 1 {
53+
// The number of right subtree node
54+
let j = n - i - 1;
55+
for left in Self::dfs(i, record_vec) {
56+
for right in Self::dfs(j, record_vec) {
57+
// Construct the ret vector
58+
ret_vec.push(Some(Rc::new(RefCell::new(TreeNode::new_with_node(left.clone(), right.clone())))));
59+
}
60+
}
61+
}
62+
63+
record_vec[n as usize] = ret_vec;
64+
65+
record_vec[n as usize].clone()
66+
}
67+
}

0 commit comments

Comments
 (0)