Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions solution/0000-0099/0099.Recover Binary Search Tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,52 @@ public class Solution {
}
```

### **JavaScript**

```javascript
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {void} Do not return anything, modify root in-place instead.
*/
const recoverTree = root => {
const data = {
prev: null,
first: null,
second: null
};
let tmp = 0;

helper(root, data);

tmp = data.first.val;
data.first.val = data.second.val;
data.second.val = tmp;
};

const helper = (root, data) => {
if (!root) return;

helper(root.left, data);

if (data.prev && data.prev.val >= root.val) {
if (!data.first) data.first = data.prev;
data.second = root;
}

data.prev = root;

helper(root.right, data);
};
```

### **...**

```
Expand Down
46 changes: 46 additions & 0 deletions solution/0000-0099/0099.Recover Binary Search Tree/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,52 @@ public class Solution {
}
```

### **JavaScript**

```javascript
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {void} Do not return anything, modify root in-place instead.
*/
const recoverTree = root => {
const data = {
prev: null,
first: null,
second: null
};
let tmp = 0;

helper(root, data);

tmp = data.first.val;
data.first.val = data.second.val;
data.second.val = tmp;
};

const helper = (root, data) => {
if (!root) return;

helper(root.left, data);

if (data.prev && data.prev.val >= root.val) {
if (!data.first) data.first = data.prev;
data.second = root;
}

data.prev = root;

helper(root.right, data);
};
```

### **...**

```
Expand Down
41 changes: 41 additions & 0 deletions solution/0000-0099/0099.Recover Binary Search Tree/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {void} Do not return anything, modify root in-place instead.
*/
const recoverTree = root => {
const data = {
prev: null,
first: null,
second: null
};
let tmp = 0;

helper(root, data);

tmp = data.first.val;
data.first.val = data.second.val;
data.second.val = tmp;
};

const helper = (root, data) => {
if (!root) return;

helper(root.left, data);

if (data.prev && data.prev.val >= root.val) {
if (!data.first) data.first = data.prev;
data.second = root;
}

data.prev = root;

helper(root.right, data);
};