-
Notifications
You must be signed in to change notification settings - Fork 0
/
99.cpp
32 lines (29 loc) · 754 Bytes
/
99.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
class Solution {
public:
void solve(TreeNode* root,TreeNode* &prev, TreeNode * &front ,TreeNode* &end){
if(root==NULL){
return;
}
solve(root->left,prev,front,end);
if(prev!=NULL && prev->val >root->val){
if(front==NULL){
front=prev;
}
end=root;
}
prev=root;
solve(root->right,prev,front,end);
}
void recoverTree(TreeNode* root) {
if(root==NULL){
return;
}
TreeNode * prev=NULL;
TreeNode * front=NULL;
TreeNode * end=NULL;
solve(root,prev,front,end);
int temp=front->val;
front->val=end->val;
end->val=temp;
}
};