-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathRecover_Binary_Search_Tree.cpp
More file actions
47 lines (45 loc) · 1.05 KB
/
Copy pathRecover_Binary_Search_Tree.cpp
File metadata and controls
47 lines (45 loc) · 1.05 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
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
//reviewed
class Solution {
public:
TreeNode *first;
TreeNode *second;
TreeNode *pre;
void inOrder(TreeNode *root){
if (root==NULL){return;}
else{
inOrder(root->left);
if (pre == NULL){pre = root;}
else {
if (pre->val > root->val){
if (first==NULL) {first = pre;}
second = root;
}
pre = root;
}
inOrder(root->right);
}
}
void recoverTree(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
pre = NULL;
first = NULL;
second= NULL;
inOrder(root);
int val;
val = first->val;
first->val=second->val;
second->val=val;
return;
}
};
//revisited.