-
Notifications
You must be signed in to change notification settings - Fork 0
/
recoverTree.cpp
39 lines (37 loc) · 996 Bytes
/
recoverTree.cpp
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
/**
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
TreeNode *p1 = NULL,*p2=NULL,*p3=NULL;
inorder(root,p1,p2,p3);
swap(p2->val,p3->val);
}
void inorder(TreeNode* root, TreeNode *&p1,TreeNode *&p2, TreeNode *&p3) {
if(!root) return;
inorder(root->left,p1,p2,p3);
if(p1) {
if(p1->val > root->val) {
if(p2) {
p3 = root;
return;
}
else {
p2 = p1;
p3 = root;
}
}
}
p1 = root;
inorder(root->right,p1,p2,p3);
}
};