Skip to content

Commit 0df24de

Browse files
committed
update: 99
1 parent 513b98f commit 0df24de

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
6969
| 96 | [unique-binary-search-trees](https://leetcode.com/problems/unique-binary-search-trees/) | [TypeScript](./src/unique-binary-search-trees/res.ts) | Medium |
7070
| 97 | [interleaving-string](https://leetcode.com/problems/interleaving-string/) | [TypeScript](./src/interleaving-string/res.ts) | Medium |
7171
| 98 | [Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/) | [JavaScript](./src/validate-binary-search-tree/res.js) | Medium |
72+
| 99 | [recover-binary-search-tree](https://leetcode.com/problems/recover-binary-search-tree/) | [TypeScript](./src/recover-binary-search-tree/res.ts) | Medium |
7273
| 100 | [Same Tree](https://leetcode.com/problems/same-tree/) | [JavaScript](./src/same-tree/res.js) | Easy |
7374
| 101 | [Symmetric Tree](https://leetcode.com/problems/symmetric-tree/) | [JavaScript](./src/symmetric-tree/res.js) | Easy |
7475
| 102 | [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) | [JavaScript](./src/binary-tree-level-order-traversal/res.js) | Medium |

src/recover-binary-search-tree/res.ts

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
const inorder = (root: TreeNode | null, nums: number[]) => {
16+
if (!root) {
17+
return ;
18+
}
19+
20+
inorder(root.left, nums);
21+
nums.push(root.val);
22+
inorder(root.right, nums);
23+
}
24+
25+
const findSwapTwoNums = (root: TreeNode | null, nums: number[]) => {
26+
let x = -1, y = -1;
27+
for (let i = 1; i < nums.length; i++) {
28+
if (nums[i] < nums[i-1]) {
29+
y = i;
30+
31+
if (x === -1) {
32+
x = i-1;
33+
} else {
34+
break;
35+
}
36+
}
37+
}
38+
39+
return [nums[x], nums[y]];
40+
}
41+
42+
const recover = (root: TreeNode | null, index: number, x: number, y: number) => {
43+
if (!root || index === 0) {
44+
return ;
45+
}
46+
47+
if ([x, y].includes(root.val)) {
48+
root.val = root.val === x ? y : x;
49+
index--;
50+
}
51+
52+
recover(root.left, index, x, y);
53+
recover(root.right, index, x, y);
54+
}
55+
56+
/**
57+
Do not return anything, modify root in-place instead.
58+
*/
59+
function recoverTree(root: TreeNode | null): void {
60+
const nums: number[] = [];
61+
62+
inorder(root, nums);
63+
const [x, y] = findSwapTwoNums(root, nums);
64+
recover(root, 2, x, y);
65+
};

0 commit comments

Comments
 (0)