File tree 1 file changed +14
-13
lines changed
scripts/algorithms/B/Binary Tree Level Order Traversal
1 file changed +14
-13
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 113 ms (Top 25.04%) | Memory: 44.6 MB (Top 16.66%)
1
2
var levelOrder = function ( root ) {
2
3
const result = [ ] ;
3
4
if ( root == null ) return result
@@ -8,19 +9,19 @@ var levelOrder = function(root) {
8
9
q . enqueue ( root )
9
10
10
11
while ( ! q . isEmpty ( ) ) {
11
- let levelSize = q . size ( ) ;
12
- while ( levelSize -- != 0 ) {
13
- let node = q . dequeue ( )
14
- temp . push ( node . val )
15
- // enqueue both children first,
16
- // before looking at the next dequeued item
17
- if ( node . left != null ) q . enqueue ( node . left ) ;
18
- if ( node . right != null ) q . enqueue ( node . right ) ;
19
- }
20
- result . push ( temp ) ;
21
- temp = [ ] ;
22
- lvl += 1
12
+ let levelSize = q . size ( ) ;
13
+ while ( levelSize -- != 0 ) {
14
+ let node = q . dequeue ( )
15
+ temp . push ( node . val )
16
+ // enqueue both children first,
17
+ // before looking at the next dequeued item
18
+ if ( node . left != null ) q . enqueue ( node . left ) ;
19
+ if ( node . right != null ) q . enqueue ( node . right ) ;
20
+ }
21
+ result . push ( temp ) ;
22
+ temp = [ ] ;
23
+ lvl += 1
23
24
}
24
25
25
26
return result
26
- } ;
27
+ } ;
You can’t perform that action at this time.
0 commit comments