Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0df24de

Browse files
committed
update: 99
1 parent513b98f commit0df24de

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
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

Lines changed: 65 additions & 0 deletions
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+
constinorder=(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+
constfindSwapTwoNums=(root:TreeNode|null,nums:number[])=>{
26+
letx=-1,y=-1;
27+
for(leti=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+
constrecover=(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+
functionrecoverTree(root:TreeNode|null):void{
60+
constnums: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)

[8]ページ先頭

©2009-2025 Movatter.jp