-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP99.cpp
41 lines (39 loc) · 962 Bytes
/
P99.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
40
41
#include "header.h"
class Solution {
public:
void recoverTree(TreeNode* root) {
w1(root);
w2(root);
}
void w1(TreeNode* root) {
if (!root) return;
if (root->left) w1(root->left);
if (prev)
if (prev->val>root->val) {
swap(prev->val,root->val);
s1 = true;
}
else if (s1)
return;
prev = root;
if (root->right) w1(root->right);
}
void w2(TreeNode* root) {
if (!root) return;
if (root->right) w2(root->right);
if (next)
if (next->val<root->val) {
swap(next->val,root->val);
s2 = true;
}
else if (s2)
return;
next = root;
if (root->left) w2(root->left);
}
TreeNode *prev = nullptr, *next = nullptr;
bool s1=false, s2=false;
};
int main() {
return 0;
}