Skip to content

Commit e6bc2b8

Browse files
committed
feat: add js solution to lc problem: no.545
1 parent f0edd0f commit e6bc2b8

File tree

3 files changed

+203
-0
lines changed

3 files changed

+203
-0
lines changed

solution/0500-0599/0545.Boundary of Binary Tree/README.md

+70
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,76 @@ class Solution {
205205
}
206206
```
207207

208+
209+
### **JavaScript**
210+
211+
```js
212+
/**
213+
* Definition for a binary tree node.
214+
* function TreeNode(val, left, right) {
215+
* this.val = (val===undefined ? 0 : val)
216+
* this.left = (left===undefined ? null : left)
217+
* this.right = (right===undefined ? null : right)
218+
* }
219+
*/
220+
/**
221+
* @param {TreeNode} root
222+
* @return {number[]}
223+
*/
224+
var boundaryOfBinaryTree = function (root) {
225+
let leftBoundary = function (root, res) {
226+
while (root) {
227+
let curVal = root.val;
228+
if (root.left) {
229+
root = root.left;
230+
} else if (root.right) {
231+
root = root.right;
232+
} else {
233+
break;
234+
}
235+
res.push(curVal);
236+
}
237+
}
238+
let rightBoundary = function (root, res) {
239+
let stk = [];
240+
while (root) {
241+
let curVal = root.val;
242+
if (root.right) {
243+
root = root.right;
244+
} else if (root.left) {
245+
root = root.left;
246+
} else {
247+
break;
248+
}
249+
stk.push(curVal);
250+
}
251+
let len = stk.length;
252+
for (let i = 0; i < len; i++) {
253+
res.push(stk.pop());
254+
}
255+
}
256+
let levelBoundary = function (root, res) {
257+
if (root) {
258+
levelBoundary(root.left, res);
259+
if (!root.left && !root.right) {
260+
res.push(root.val);
261+
}
262+
levelBoundary(root.right, res);
263+
}
264+
}
265+
let res = [];
266+
if (root) {
267+
res.push(root.val);
268+
leftBoundary(root.left, res);
269+
if (root.left || root.right) {
270+
levelBoundary(root, res);
271+
}
272+
rightBoundary(root.right, res);
273+
}
274+
return res;
275+
};
276+
```
277+
208278
### **...**
209279

210280
```

solution/0500-0599/0545.Boundary of Binary Tree/README_EN.md

+69
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,75 @@ class Solution {
196196
}
197197
```
198198

199+
### **JavaScript**
200+
201+
```js
202+
/**
203+
* Definition for a binary tree node.
204+
* function TreeNode(val, left, right) {
205+
* this.val = (val===undefined ? 0 : val)
206+
* this.left = (left===undefined ? null : left)
207+
* this.right = (right===undefined ? null : right)
208+
* }
209+
*/
210+
/**
211+
* @param {TreeNode} root
212+
* @return {number[]}
213+
*/
214+
var boundaryOfBinaryTree = function (root) {
215+
let leftBoundary = function (root, res) {
216+
while (root) {
217+
let curVal = root.val;
218+
if (root.left) {
219+
root = root.left;
220+
} else if (root.right) {
221+
root = root.right;
222+
} else {
223+
break;
224+
}
225+
res.push(curVal);
226+
}
227+
}
228+
let rightBoundary = function (root, res) {
229+
let stk = [];
230+
while (root) {
231+
let curVal = root.val;
232+
if (root.right) {
233+
root = root.right;
234+
} else if (root.left) {
235+
root = root.left;
236+
} else {
237+
break;
238+
}
239+
stk.push(curVal);
240+
}
241+
let len = stk.length;
242+
for (let i = 0; i < len; i++) {
243+
res.push(stk.pop());
244+
}
245+
}
246+
let levelBoundary = function (root, res) {
247+
if (root) {
248+
levelBoundary(root.left, res);
249+
if (!root.left && !root.right) {
250+
res.push(root.val);
251+
}
252+
levelBoundary(root.right, res);
253+
}
254+
}
255+
let res = [];
256+
if (root) {
257+
res.push(root.val);
258+
leftBoundary(root.left, res);
259+
if (root.left || root.right) {
260+
levelBoundary(root, res);
261+
}
262+
rightBoundary(root.right, res);
263+
}
264+
return res;
265+
};
266+
```
267+
199268
### **...**
200269

201270
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 boundaryOfBinaryTree = function (root) {
14+
let leftBoundary = function (root, res) {
15+
while (root) {
16+
let curVal = root.val;
17+
if (root.left) {
18+
root = root.left;
19+
} else if (root.right) {
20+
root = root.right;
21+
} else {
22+
break;
23+
}
24+
res.push(curVal);
25+
}
26+
}
27+
let rightBoundary = function (root, res) {
28+
let stk = [];
29+
while (root) {
30+
let curVal = root.val;
31+
if (root.right) {
32+
root = root.right;
33+
} else if (root.left) {
34+
root = root.left;
35+
} else {
36+
break;
37+
}
38+
stk.push(curVal);
39+
}
40+
let len = stk.length;
41+
for (let i = 0; i < len; i++) {
42+
res.push(stk.pop());
43+
}
44+
}
45+
let levelBoundary = function (root, res) {
46+
if (root) {
47+
levelBoundary(root.left, res);
48+
if (!root.left && !root.right) {
49+
res.push(root.val);
50+
}
51+
levelBoundary(root.right, res);
52+
}
53+
}
54+
let res = [];
55+
if (root) {
56+
res.push(root.val);
57+
leftBoundary(root.left, res);
58+
if (root.left || root.right) {
59+
levelBoundary(root, res);
60+
}
61+
rightBoundary(root.right, res);
62+
}
63+
return res;
64+
};

0 commit comments

Comments
 (0)