Skip to content

Commit 5f4b0ed

Browse files
authored
feat: add rust solution to lc problem: No.0107 (doocs#1279)
1 parent 36a5d30 commit 5f4b0ed

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed

solution/0100-0199/0107.Binary Tree Level Order Traversal II/README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,65 @@ public:
162162
};
163163
```
164164
165+
### **Rust**
166+
167+
```rust
168+
// Definition for a binary tree node.
169+
// #[derive(Debug, PartialEq, Eq)]
170+
// pub struct TreeNode {
171+
// pub val: i32,
172+
// pub left: Option<Rc<RefCell<TreeNode>>>,
173+
// pub right: Option<Rc<RefCell<TreeNode>>>,
174+
// }
175+
//
176+
// impl TreeNode {
177+
// #[inline]
178+
// pub fn new(val: i32) -> Self {
179+
// TreeNode {
180+
// val,
181+
// left: None,
182+
// right: None
183+
// }
184+
// }
185+
// }
186+
use std::{rc::Rc, cell::RefCell, collections::VecDeque};
187+
impl Solution {
188+
#[allow(dead_code)]
189+
pub fn level_order_bottom(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<i32>> {
190+
if root.is_none() {
191+
return vec![];
192+
}
193+
let mut ret_vec = Vec::new();
194+
let mut q = VecDeque::new();
195+
196+
q.push_back(root);
197+
198+
while !q.is_empty() {
199+
let mut cur_vec = Vec::new();
200+
let mut next_q = VecDeque::new();
201+
while !q.is_empty() {
202+
let cur_front = q.front().unwrap().clone();
203+
q.pop_front();
204+
cur_vec.push(cur_front.as_ref().unwrap().borrow().val);
205+
let left = cur_front.as_ref().unwrap().borrow().left.clone();
206+
let right = cur_front.as_ref().unwrap().borrow().right.clone();
207+
if !left.is_none() {
208+
next_q.push_back(left);
209+
}
210+
if !right.is_none() {
211+
next_q.push_back(right);
212+
}
213+
}
214+
ret_vec.push(cur_vec);
215+
q = next_q;
216+
}
217+
218+
ret_vec.reverse();
219+
ret_vec
220+
}
221+
}
222+
```
223+
165224
### **Go**
166225

167226
```go

solution/0100-0199/0107.Binary Tree Level Order Traversal II/README_EN.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,65 @@ 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+
use std::{rc::Rc, cell::RefCell, collections::VecDeque};
175+
impl Solution {
176+
#[allow(dead_code)]
177+
pub fn level_order_bottom(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<i32>> {
178+
if root.is_none() {
179+
return vec![];
180+
}
181+
let mut ret_vec = Vec::new();
182+
let mut q = VecDeque::new();
183+
184+
q.push_back(root);
185+
186+
while !q.is_empty() {
187+
let mut cur_vec = Vec::new();
188+
let mut next_q = VecDeque::new();
189+
while !q.is_empty() {
190+
let cur_front = q.front().unwrap().clone();
191+
q.pop_front();
192+
cur_vec.push(cur_front.as_ref().unwrap().borrow().val);
193+
let left = cur_front.as_ref().unwrap().borrow().left.clone();
194+
let right = cur_front.as_ref().unwrap().borrow().right.clone();
195+
if !left.is_none() {
196+
next_q.push_back(left);
197+
}
198+
if !right.is_none() {
199+
next_q.push_back(right);
200+
}
201+
}
202+
ret_vec.push(cur_vec);
203+
q = next_q;
204+
}
205+
206+
ret_vec.reverse();
207+
ret_vec
208+
}
209+
}
210+
```
211+
153212
### **Go**
154213

155214
```go
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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, cell::RefCell, collections::VecDeque};
20+
impl Solution {
21+
#[allow(dead_code)]
22+
pub fn level_order_bottom(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<i32>> {
23+
if root.is_none() {
24+
return vec![];
25+
}
26+
let mut ret_vec = Vec::new();
27+
let mut q = VecDeque::new();
28+
29+
q.push_back(root);
30+
31+
while !q.is_empty() {
32+
let mut cur_vec = Vec::new();
33+
let mut next_q = VecDeque::new();
34+
while !q.is_empty() {
35+
let cur_front = q.front().unwrap().clone();
36+
q.pop_front();
37+
cur_vec.push(cur_front.as_ref().unwrap().borrow().val);
38+
let left = cur_front.as_ref().unwrap().borrow().left.clone();
39+
let right = cur_front.as_ref().unwrap().borrow().right.clone();
40+
if !left.is_none() {
41+
next_q.push_back(left);
42+
}
43+
if !right.is_none() {
44+
next_q.push_back(right);
45+
}
46+
}
47+
ret_vec.push(cur_vec);
48+
q = next_q;
49+
}
50+
51+
ret_vec.reverse();
52+
ret_vec
53+
}
54+
}

0 commit comments

Comments
 (0)