-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecoverBinarySearchTree.cpp
More file actions
56 lines (49 loc) · 1.3 KB
/
RecoverBinarySearchTree.cpp
File metadata and controls
56 lines (49 loc) · 1.3 KB
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
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
/***
* 二叉查找树中序遍历是升序的
* 中序遍历时记录前一个节点,当乱序(当前结点值 < 前一个节点值)时,需要调整顺序
* 如果遍历时只出现一个乱序点,说明乱序的两个节点相邻;否则有两个乱序点
***/
class Solution {
public:
void recoverTree(TreeNode *root) {
if (NULL == root)
return;
prenode = NULL;
pm1 = NULL;
pm2 = NULL;
inorderTree(root);
swap(pm1->val, pm2->val);
return;
}
private:
void inorderTree(TreeNode *root)
{
if (NULL == root)
return;
inorderTree(root->left);
if ((NULL != prenode) && (root->val < prenode->val))
{
if (NULL == pm1) // 第一次乱序
{
pm1 = prenode; // prenode一定是乱序点
pm2 = root; // 当前点root可能是(相邻),也可能不是
}
else
pm2 = root; // 第二次乱序
}
prenode = root; // set prenode
inorderTree(root->right);
}
TreeNode *prenode;
TreeNode *pm1; // mistake
TreeNode *pm2;
};