Skip to content

Commit f3483c4

Browse files
authored
feat: add javascript solution to lc problem: No.0099 (doocs#694)
1 parent 666affc commit f3483c4

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

solution/0000-0099/0099.Recover Binary Search Tree/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,52 @@ public class Solution {
252252
}
253253
```
254254

255+
### **JavaScript**
256+
257+
```javascript
258+
/**
259+
* Definition for a binary tree node.
260+
* function TreeNode(val, left, right) {
261+
* this.val = (val===undefined ? 0 : val)
262+
* this.left = (left===undefined ? null : left)
263+
* this.right = (right===undefined ? null : right)
264+
* }
265+
*/
266+
/**
267+
* @param {TreeNode} root
268+
* @return {void} Do not return anything, modify root in-place instead.
269+
*/
270+
const recoverTree = root => {
271+
const data = {
272+
prev: null,
273+
first: null,
274+
second: null
275+
};
276+
let tmp = 0;
277+
278+
helper(root, data);
279+
280+
tmp = data.first.val;
281+
data.first.val = data.second.val;
282+
data.second.val = tmp;
283+
};
284+
285+
const helper = (root, data) => {
286+
if (!root) return;
287+
288+
helper(root.left, data);
289+
290+
if (data.prev && data.prev.val >= root.val) {
291+
if (!data.first) data.first = data.prev;
292+
data.second = root;
293+
}
294+
295+
data.prev = root;
296+
297+
helper(root.right, data);
298+
};
299+
```
300+
255301
### **...**
256302

257303
```

solution/0000-0099/0099.Recover Binary Search Tree/README_EN.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,52 @@ public class Solution {
243243
}
244244
```
245245

246+
### **JavaScript**
247+
248+
```javascript
249+
/**
250+
* Definition for a binary tree node.
251+
* function TreeNode(val, left, right) {
252+
* this.val = (val===undefined ? 0 : val)
253+
* this.left = (left===undefined ? null : left)
254+
* this.right = (right===undefined ? null : right)
255+
* }
256+
*/
257+
/**
258+
* @param {TreeNode} root
259+
* @return {void} Do not return anything, modify root in-place instead.
260+
*/
261+
const recoverTree = root => {
262+
const data = {
263+
prev: null,
264+
first: null,
265+
second: null
266+
};
267+
let tmp = 0;
268+
269+
helper(root, data);
270+
271+
tmp = data.first.val;
272+
data.first.val = data.second.val;
273+
data.second.val = tmp;
274+
};
275+
276+
const helper = (root, data) => {
277+
if (!root) return;
278+
279+
helper(root.left, data);
280+
281+
if (data.prev && data.prev.val >= root.val) {
282+
if (!data.first) data.first = data.prev;
283+
data.second = root;
284+
}
285+
286+
data.prev = root;
287+
288+
helper(root.right, data);
289+
};
290+
```
291+
246292
### **...**
247293

248294
```
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 {void} Do not return anything, modify root in-place instead.
12+
*/
13+
const recoverTree = root => {
14+
const data = {
15+
prev: null,
16+
first: null,
17+
second: null
18+
};
19+
let tmp = 0;
20+
21+
helper(root, data);
22+
23+
tmp = data.first.val;
24+
data.first.val = data.second.val;
25+
data.second.val = tmp;
26+
};
27+
28+
const helper = (root, data) => {
29+
if (!root) return;
30+
31+
helper(root.left, data);
32+
33+
if (data.prev && data.prev.val >= root.val) {
34+
if (!data.first) data.first = data.prev;
35+
data.second = root;
36+
}
37+
38+
data.prev = root;
39+
40+
helper(root.right, data);
41+
};

0 commit comments

Comments
 (0)