@@ -148,8 +148,12 @@ public:
148
148
auto node = q.front();
149
149
q.pop();
150
150
t.push_back(node->val);
151
- if (node->left) q.push(node->left);
152
- if (node->right) q.push(node->right);
151
+ if (node->left) {
152
+ q.push(node->left);
153
+ }
154
+ if (node->right) {
155
+ q.push(node->right);
156
+ }
153
157
}
154
158
ans.push_back(t);
155
159
}
@@ -207,23 +211,23 @@ func levelOrder(root *TreeNode) (ans [][]int) {
207
211
*/
208
212
209
213
function levelOrder(root : TreeNode | null ): number [][] {
210
- const res = [];
211
- if (root == null ) {
212
- return res ;
214
+ const ans : number [][] = [];
215
+ if (! root ) {
216
+ return ans ;
213
217
}
214
- const queue = [root ];
215
- while (queue .length != 0 ) {
216
- const n = queue . length ;
217
- res . push (
218
- new Array ( n ). fill ( null ). map (() => {
219
- const { val, left, right } = queue . shift ( );
220
- left && queue .push (left );
221
- right && queue .push (right );
222
- return val ;
223
- }),
224
- );
218
+ const q : TreeNode [] = [root ];
219
+ while (q .length ) {
220
+ const t : number [] = [] ;
221
+ const qq : TreeNode [] = [];
222
+ for ( const { val, left, right } of q ) {
223
+ t . push ( val );
224
+ left && qq .push (left );
225
+ right && qq .push (right );
226
+ }
227
+ ans . push ( t );
228
+ q . splice ( 0 , q . length , ... qq );
225
229
}
226
- return res ;
230
+ return ans ;
227
231
}
228
232
```
229
233
@@ -248,34 +252,31 @@ function levelOrder(root: TreeNode | null): number[][] {
248
252
// }
249
253
use std :: rc :: Rc ;
250
254
use std :: cell :: RefCell ;
251
- use std :: collections :: VecDeque ;
255
+ use std :: collections :: { VecDeque } ;
252
256
impl Solution {
253
257
pub fn level_order (root : Option <Rc <RefCell <TreeNode >>>) -> Vec <Vec <i32 >> {
254
- let mut res = vec! [];
255
- if root . is_none () {
256
- return res ;
257
- }
258
- let mut queue : VecDeque <Option <Rc <RefCell <TreeNode >>>> = vec! [root ]. into_iter (). collect ();
259
- while ! queue . is_empty () {
260
- let n = queue . len ();
261
- res . push (
262
- (0 .. n )
263
- . into_iter ()
264
- . map (| _ | {
265
- let mut node = queue . pop_front (). unwrap ();
266
- let mut node = node . as_mut (). unwrap (). borrow_mut ();
267
- if node . left. is_some () {
268
- queue . push_back (node . left. take ());
258
+ let mut ans = Vec :: new ();
259
+ if let Some (root_node ) = root {
260
+ let mut q = VecDeque :: new ();
261
+ q . push_back (root_node );
262
+ while ! q . is_empty () {
263
+ let mut t = Vec :: new ();
264
+ for _ in 0 .. q . len () {
265
+ if let Some (node ) = q . pop_front () {
266
+ let node_ref = node . borrow ();
267
+ t . push (node_ref . val);
268
+ if let Some (ref left ) = node_ref . left {
269
+ q . push_back (Rc :: clone (left ));
269
270
}
270
- if node . right . is_some () {
271
- queue . push_back (node . right . take ( ));
271
+ if let Some ( ref right ) = node_ref . right {
272
+ q . push_back (Rc :: clone ( right ));
272
273
}
273
- node . val
274
- })
275
- . collect ()
276
- );
274
+ }
275
+ }
276
+ ans . push ( t );
277
+ }
277
278
}
278
- res
279
+ ans
279
280
}
280
281
}
281
282
```
@@ -294,20 +295,21 @@ impl Solution {
294
295
* @return {number[][]}
295
296
*/
296
297
var levelOrder = function (root ) {
297
- let ans = [];
298
+ const ans = [];
298
299
if (! root) {
299
300
return ans;
300
301
}
301
- let q = [root];
302
+ const q = [root];
302
303
while (q .length ) {
303
- let t = [];
304
- for ( let n = q . length ; n; -- n) {
305
- const { val , left , right } = q . shift ();
304
+ const t = [];
305
+ const qq = [];
306
+ for ( const { val , left , right } of q) {
306
307
t .push (val);
307
- left && q .push (left);
308
- right && q .push (right);
308
+ left && qq .push (left);
309
+ right && qq .push (right);
309
310
}
310
311
ans .push (t);
312
+ q .splice (0 , q .length , ... qq);
311
313
}
312
314
return ans;
313
315
};
0 commit comments