Skip to content

Commit ba7ac63

Browse files
author
chenzhao
committed
feat(#0099): add #0099 Recover Binary Search Tree, Inorder Traversal
1 parent 11577d6 commit ba7ac63

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// Two elements of a binary search tree (BST) are swapped by mistake.
2+
3+
// Recover the tree without changing its structure.
4+
5+
// Example 1:
6+
7+
// Input: [1,3,null,null,2]
8+
9+
// 1
10+
// /
11+
// 3
12+
// \
13+
// 2
14+
15+
// Output: [3,1,null,null,2]
16+
17+
// 3
18+
// /
19+
// 1
20+
// \
21+
// 2
22+
// Example 2:
23+
24+
// Input: [3,1,4,null,null,2]
25+
26+
// 3
27+
// / \
28+
// 1 4
29+
// /
30+
// 2
31+
32+
// Output: [2,1,4,null,null,3]
33+
34+
// 2
35+
// / \
36+
// 1 4
37+
// /
38+
// 3
39+
40+
// Follow up:
41+
42+
// A solution using O(n) space is pretty straight forward.
43+
// Could you devise a constant space solution?
44+
45+
46+
// 1) 顺序遍历
47+
// https://leetcode.com/problems/recover-binary-search-tree/discuss/32535/No-Fancy-Algorithm-just-Simple-and-Powerful-In-Order-Traversal
48+
// 找到问题的思路,如果按顺序遍历,节点值一定是从小到大的排列顺序,例如错误的二叉搜索树 [3,1,4,null,null,2],顺序遍历的结果是:[1,3,2,4],正确的二叉搜索树遍历的结果应该是:[1,2,3,4]。如果只有两个元素被交换了,那么只需找那两个元素。一旦有两个元素被交换了,那么一定存在一个节点值小于后面的结点值,对于前面的例子,就是 2 < 3。
49+
// 找到这两个元素的方法是:pre 是前一个节点,1)如果当前节点小于 pre,需要交换的第一个元素 first 就是 pre,2)如果确定了第一个元素,当前节点小于 pre,需要交换的第二个元素 second 就是当前节点。注意需要遍历完整个顺序列表才能确定需要交换的两个元素。
50+
/**
51+
* Definition for a binary tree node.
52+
* function TreeNode(val) {
53+
* this.val = val;
54+
* this.left = this.right = null;
55+
* }
56+
*/
57+
/**
58+
* @param {TreeNode} root
59+
* @return {void} Do not return anything, modify root in-place instead.
60+
*/
61+
function TreeNode(val) {
62+
this.val = val
63+
this.left = null
64+
this.right = null
65+
}
66+
const recoverTree = (root) => {
67+
let first = null
68+
let second = null
69+
let pre = new TreeNode(-Infinity)
70+
// 順序遍歷
71+
inorder(root)
72+
// 交換錯位的兩個節點
73+
if (first && second) {
74+
let temp = first.val
75+
first.val = second.val
76+
second.val = temp
77+
}
78+
function inorder(root) {
79+
if (!root) {
80+
return
81+
}
82+
inorder(root.left)
83+
if (!first && pre.val >= root.val) {
84+
first = pre
85+
}
86+
if (first && pre.val >= root.val) {
87+
second = root
88+
}
89+
pre = root
90+
inorder(root.right)
91+
}
92+
return root
93+
}
94+
// Runtime: 128 ms, faster than 47.30% of JavaScript online submissions for Recover Binary Search Tree.
95+
// Memory Usage: 40.9 MB, less than 50.00% of JavaScript online submissions for Recover Binary Search Tree.
96+
97+
// Test case:
98+
// let root = {
99+
// val: 3,
100+
// left: {
101+
// val: 1,
102+
// left: null,
103+
// right: null
104+
// },
105+
// right: {
106+
// val: 4,
107+
// left: {
108+
// val: 2,
109+
// left: null,
110+
// right: null
111+
// },
112+
// right: null
113+
// }
114+
// }
115+
// let root = {
116+
// val: 2,
117+
// left: {
118+
// val: 3,
119+
// left: null,
120+
// right: null
121+
// },
122+
// right: {
123+
// val: 1,
124+
// left: null,
125+
// right: null
126+
// }
127+
// }
128+
// let root = {
129+
// val: 2,
130+
// left: {
131+
// val: 1,
132+
// left: null,
133+
// right: null
134+
// },
135+
// right: null
136+
// }
137+
// console.log(recoverTree(root))
138+

0 commit comments

Comments
 (0)