File tree Expand file tree Collapse file tree 3 files changed +127
-0
lines changed
solution/0100-0199/0107.Binary Tree Level Order Traversal II Expand file tree Collapse file tree 3 files changed +127
-0
lines changed Original file line number Diff line number Diff line change @@ -106,6 +106,50 @@ class Solution {
106
106
}
107
107
```
108
108
109
+ ### ** JavaScript**
110
+
111
+ ``` js
112
+ /**
113
+ * Definition for a binary tree node.
114
+ * function TreeNode(val, left, right) {
115
+ * this.val = (val===undefined ? 0 : val)
116
+ * this.left = (left===undefined ? null : left)
117
+ * this.right = (right===undefined ? null : right)
118
+ * }
119
+ */
120
+ /**
121
+ * @param {TreeNode} root
122
+ * @return {number[][]}
123
+ */
124
+ var levelOrderBottom = function (root ) {
125
+ if (! root) {
126
+ return [];
127
+ }
128
+ let q = [], output = [], levelOutput = [];
129
+ q .push (root);
130
+ q .push (null );
131
+ while (q .length ) {
132
+ let cur = q .shift ();
133
+ levelOutput .push (cur .val );
134
+ if (cur .left ) {
135
+ q .push (cur .left );
136
+ }
137
+ if (cur .right ) {
138
+ q .push (cur .right );
139
+ }
140
+ if (! q[0 ]) {
141
+ q .shift ();
142
+ output .unshift (levelOutput);
143
+ levelOutput = [];
144
+ if (q .length ) {
145
+ q .push (null );
146
+ }
147
+ }
148
+ }
149
+ return output;
150
+ };
151
+ ```
152
+
109
153
### ** ...**
110
154
111
155
```
Original file line number Diff line number Diff line change @@ -105,6 +105,50 @@ class Solution {
105
105
}
106
106
```
107
107
108
+ ### ** JavaScript**
109
+
110
+ ``` js
111
+ /**
112
+ * Definition for a binary tree node.
113
+ * function TreeNode(val, left, right) {
114
+ * this.val = (val===undefined ? 0 : val)
115
+ * this.left = (left===undefined ? null : left)
116
+ * this.right = (right===undefined ? null : right)
117
+ * }
118
+ */
119
+ /**
120
+ * @param {TreeNode} root
121
+ * @return {number[][]}
122
+ */
123
+ var levelOrderBottom = function (root ) {
124
+ if (! root) {
125
+ return [];
126
+ }
127
+ let q = [], output = [], levelOutput = [];
128
+ q .push (root);
129
+ q .push (null );
130
+ while (q .length ) {
131
+ let cur = q .shift ();
132
+ levelOutput .push (cur .val );
133
+ if (cur .left ) {
134
+ q .push (cur .left );
135
+ }
136
+ if (cur .right ) {
137
+ q .push (cur .right );
138
+ }
139
+ if (! q[0 ]) {
140
+ q .shift ();
141
+ output .unshift (levelOutput);
142
+ levelOutput = [];
143
+ if (q .length ) {
144
+ q .push (null );
145
+ }
146
+ }
147
+ }
148
+ return output;
149
+ };
150
+ ```
151
+
108
152
### ** ...**
109
153
110
154
```
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * function TreeNode(val, left, right) {
4
+ * this.val = (val===undefined ? 0 : val)
5
+ * this.left = (left===undefined ? null : left)
6
+ * this.right = (right===undefined ? null : right)
7
+ * }
8
+ */
9
+ /**
10
+ * @param {TreeNode } root
11
+ * @return {number[][] }
12
+ */
13
+ var levelOrderBottom = function ( root ) {
14
+ if ( ! root ) {
15
+ return [ ] ;
16
+ }
17
+ let q = [ ] , output = [ ] , levelOutput = [ ] ;
18
+ q . push ( root ) ;
19
+ q . push ( null ) ;
20
+ while ( q . length ) {
21
+ let cur = q . shift ( ) ;
22
+ levelOutput . push ( cur . val ) ;
23
+ if ( cur . left ) {
24
+ q . push ( cur . left ) ;
25
+ }
26
+ if ( cur . right ) {
27
+ q . push ( cur . right ) ;
28
+ }
29
+ if ( ! q [ 0 ] ) {
30
+ q . shift ( ) ;
31
+ output . unshift ( levelOutput ) ;
32
+ levelOutput = [ ] ;
33
+ if ( q . length ) {
34
+ q . push ( null ) ;
35
+ }
36
+ }
37
+ }
38
+ return output ;
39
+ } ;
You can’t perform that action at this time.
0 commit comments