-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path099. 恢复二叉搜索树.js
105 lines (88 loc) · 2.25 KB
/
099. 恢复二叉搜索树.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* Created by admin on 2018/10/19.
*/
/**二叉搜索树中的两个节点被错误地交换。
* 请在不改变其结构的情况下,恢复这棵树。
* 输入: [3,1,4,null,null,2]
* 3
* / \
* 1 4
* /
* 2
* 输出: [2,1,4,null,null,3]
* 2
* / \
* 1 4
* /
* 3
* 进阶:
* 使用 O(n) 空间复杂度的解法很容易实现。
* 你能想出一个只使用常数空间的解决方案吗?
*/
function TreeNode(val) {
this.val = val;
this.left = this.right = null;
}
/**对二叉树进行左中右遍历即可得搜索树的升序排列
* 如果一个升序数组有2个数进行了交换
* 那么观察可知,一定有2次降序的地方
* 那么被交换的数一定是第一次降序的第一个数,和第二次降序的第二个数
*
* @param {TreeNode} root
* @return {void} Do not return anything, modify root in-place instead.
*/
var recoverTree = function (root) {
let pre = null;
let s1 = null;
let s2 = null;
function _traversal(root) {
if (root === null) return;
_traversal(root.left);
if (pre !== null && pre.val > root.val) {
if (s1 === null) {
s1 = pre;
}
s2 = root;
}
pre = root;
_traversal(root.right);
}
_traversal(root);
if (s1 != null && s2 != null) {
var temp = s1.val;
s1.val = s2.val;
s2.val = temp;
}
};
recoverTree = function (root) {
if (root === null) return;
let stack = [];
let head = root;
let pre = null;
let s1 = null;
let s2 = null;
while (stack.length || head !== null) {
if (head !== null) {
stack.push(head)
head = head.left;
} else {
head = stack.pop();
if (pre !== null && pre.val > head.val) {
if (s1 === null) {
s1 = pre;
}
s2 = head;
}
pre = head;
head = head.right;
}
}
if (s1 != null && s2 != null) {
var temp = s1.val;
s1.val = s2.val;
s2.val = temp;
}
// console.log(root);
};
const node = require('./base/nodeTree').searchTree;
recoverTree(node)