Skip to content

Commit 684ee50

Browse files
authored
feat: add rust solution to lc problem: No.0114 (#1280)
1 parent bd0a467 commit 684ee50

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed

solution/0100-0199/0114.Flatten Binary Tree to Linked List/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,60 @@ public:
166166
};
167167
```
168168
169+
### **Rust**
170+
171+
```rust
172+
// Definition for a binary tree node.
173+
// #[derive(Debug, PartialEq, Eq)]
174+
// pub struct TreeNode {
175+
// pub val: i32,
176+
// pub left: Option<Rc<RefCell<TreeNode>>>,
177+
// pub right: Option<Rc<RefCell<TreeNode>>>,
178+
// }
179+
//
180+
// impl TreeNode {
181+
// #[inline]
182+
// pub fn new(val: i32) -> Self {
183+
// TreeNode {
184+
// val,
185+
// left: None,
186+
// right: None
187+
// }
188+
// }
189+
// }
190+
use std::rc::Rc;
191+
use std::cell::RefCell;
192+
impl Solution {
193+
#[allow(dead_code)]
194+
pub fn flatten(root: &mut Option<Rc<RefCell<TreeNode>>>) {
195+
if root.is_none() {
196+
return;
197+
}
198+
let mut v: Vec<Option<Rc<RefCell<TreeNode>>>> = Vec::new();
199+
// Initialize the vector
200+
Self::pre_order_traverse(&mut v, root);
201+
// Traverse the vector
202+
let n = v.len();
203+
for i in 0..n - 1 {
204+
v[i].as_ref().unwrap().borrow_mut().left = None;
205+
v[i].as_ref().unwrap().borrow_mut().right = v[i + 1].clone();
206+
}
207+
}
208+
209+
#[allow(dead_code)]
210+
fn pre_order_traverse(v: &mut Vec<Option<Rc<RefCell<TreeNode>>>>, root: &Option<Rc<RefCell<TreeNode>>>) {
211+
if root.is_none() {
212+
return;
213+
}
214+
v.push(root.clone());
215+
let left = root.as_ref().unwrap().borrow().left.clone();
216+
let right = root.as_ref().unwrap().borrow().right.clone();
217+
Self::pre_order_traverse(v, &left);
218+
Self::pre_order_traverse(v, &right);
219+
}
220+
}
221+
```
222+
169223
### **Go**
170224

171225
```go

solution/0100-0199/0114.Flatten Binary Tree to Linked List/README_EN.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,60 @@ public:
142142
};
143143
```
144144
145+
### **Rust**
146+
147+
```rust
148+
// Definition for a binary tree node.
149+
// #[derive(Debug, PartialEq, Eq)]
150+
// pub struct TreeNode {
151+
// pub val: i32,
152+
// pub left: Option<Rc<RefCell<TreeNode>>>,
153+
// pub right: Option<Rc<RefCell<TreeNode>>>,
154+
// }
155+
//
156+
// impl TreeNode {
157+
// #[inline]
158+
// pub fn new(val: i32) -> Self {
159+
// TreeNode {
160+
// val,
161+
// left: None,
162+
// right: None
163+
// }
164+
// }
165+
// }
166+
use std::rc::Rc;
167+
use std::cell::RefCell;
168+
impl Solution {
169+
#[allow(dead_code)]
170+
pub fn flatten(root: &mut Option<Rc<RefCell<TreeNode>>>) {
171+
if root.is_none() {
172+
return;
173+
}
174+
let mut v: Vec<Option<Rc<RefCell<TreeNode>>>> = Vec::new();
175+
// Initialize the vector
176+
Self::pre_order_traverse(&mut v, root);
177+
// Traverse the vector
178+
let n = v.len();
179+
for i in 0..n - 1 {
180+
v[i].as_ref().unwrap().borrow_mut().left = None;
181+
v[i].as_ref().unwrap().borrow_mut().right = v[i + 1].clone();
182+
}
183+
}
184+
185+
#[allow(dead_code)]
186+
fn pre_order_traverse(v: &mut Vec<Option<Rc<RefCell<TreeNode>>>>, root: &Option<Rc<RefCell<TreeNode>>>) {
187+
if root.is_none() {
188+
return;
189+
}
190+
v.push(root.clone());
191+
let left = root.as_ref().unwrap().borrow().left.clone();
192+
let right = root.as_ref().unwrap().borrow().right.clone();
193+
Self::pre_order_traverse(v, &left);
194+
Self::pre_order_traverse(v, &right);
195+
}
196+
}
197+
```
198+
145199
### **Go**
146200

147201
```go
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
use std::rc::Rc;
20+
use std::cell::RefCell;
21+
impl Solution {
22+
#[allow(dead_code)]
23+
pub fn flatten(root: &mut Option<Rc<RefCell<TreeNode>>>) {
24+
if root.is_none() {
25+
return;
26+
}
27+
let mut v: Vec<Option<Rc<RefCell<TreeNode>>>> = Vec::new();
28+
// Initialize the vector
29+
Self::pre_order_traverse(&mut v, root);
30+
// Traverse the vector
31+
let n = v.len();
32+
for i in 0..n - 1 {
33+
v[i].as_ref().unwrap().borrow_mut().left = None;
34+
v[i].as_ref().unwrap().borrow_mut().right = v[i + 1].clone();
35+
}
36+
}
37+
38+
#[allow(dead_code)]
39+
fn pre_order_traverse(v: &mut Vec<Option<Rc<RefCell<TreeNode>>>>, root: &Option<Rc<RefCell<TreeNode>>>) {
40+
if root.is_none() {
41+
return;
42+
}
43+
v.push(root.clone());
44+
let left = root.as_ref().unwrap().borrow().left.clone();
45+
let right = root.as_ref().unwrap().borrow().right.clone();
46+
Self::pre_order_traverse(v, &left);
47+
Self::pre_order_traverse(v, &right);
48+
}
49+
}

0 commit comments

Comments
 (0)