Skip to content

Commit db9136f

Browse files
committed
Add js solution to lc problem: no.107
1 parent 6cc160e commit db9136f

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,50 @@ class Solution {
106106
}
107107
```
108108

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+
109153
### **...**
110154

111155
```

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,50 @@ class Solution {
105105
}
106106
```
107107

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+
108152
### **...**
109153

110154
```
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
};

0 commit comments

Comments
 (0)