-
Notifications
You must be signed in to change notification settings - Fork 71
/
No99.recover-binary-search-tree.js
83 lines (73 loc) · 1.59 KB
/
No99.recover-binary-search-tree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Difficulty:
* Hard
*
* Desc:
* Two elements of a binary search tree (BST) are swapped by mistake.
* Recover the tree without changing its structure.
*
* Example1:
* Input: [1,3,null,null,2]
1
/
3
\
2
Output: [3,1,null,null,2]
3
/
1
\
2
*
* Example2:
* Input: [3,1,4,null,null,2]
3
/ \
1 4
/
2
Output: [2,1,4,null,null,3]
2
/ \
1 4
/
3
*
* Note:
* A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?
*/
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {void} Do not return anything, modify root in-place instead.
*
* 对二叉搜索树进行 中序遍历的时候 访问到的元素是从小到大顺序排列的
* 如我们对例 2 恢复好的树 进行中序遍历 得到的应该是 1 2 3 4
* 在对错误的树进行中序遍历的时候,那如果对两个节点交换了顺序,
* 那一定有两个地方是不满足:前一个元素 < 当前元素 < 后一个元素
*/
var recoverTree = function(root) {
let pre = null
let node1 = null
let node2 = null
const inorderTraversal = (node) => {
if (node.left) inorderTraversal(node.left)
if (pre && pre.val > node.val) {
if (!node1) node1 = pre
node2 = node
}
pre = node
if (node.right) inorderTraversal(node.right)
}
inorderTraversal(root)
const tmp = node1.val
node1.val = node2.val
node2.val = tmp
}