File tree Expand file tree Collapse file tree 3 files changed +172
-0
lines changed
solution/0100-0199/0107.Binary Tree Level Order Traversal II Expand file tree Collapse file tree 3 files changed +172
-0
lines changed Original file line number Diff line number Diff line change @@ -162,6 +162,65 @@ public:
162
162
};
163
163
```
164
164
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
+
165
224
### ** Go**
166
225
167
226
``` go
Original file line number Diff line number Diff line change @@ -150,6 +150,65 @@ public:
150
150
};
151
151
```
152
152
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
+
153
212
### ** Go**
154
213
155
214
``` go
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments