-
Notifications
You must be signed in to change notification settings - Fork 1
/
099.recoverBST.cpp
44 lines (40 loc) · 1.27 KB
/
099.recoverBST.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
42
43
44
// O(N), inorder traversal, 2 O(N) space vectors
class Solution {
public:
void recoverTree(TreeNode* root) {
vector<TreeNode*> nodes;
vector<int> vals;
inorder(root, nodes, vals);
sort(vals.begin(), vals.end());
for (int i = 0; i < vals.size(); i++) {
nodes[i]->val = vals[i];
}
}
void inorder(TreeNode* root, vector<TreeNode*>& nodes, vector<int>& vals) {
if (!root) return ;
inorder(root->left, nodes, vals);
nodes.push_back(root);
vals.push_back(root->val);
inorder(root->right, nodes, vals);
}
};
// O(1) extra space: find the two nodes and swap
class Solution {
public:
TreeNode* first = NULL;
TreeNode* second = NULL;
TreeNode* prev = new TreeNode(INT_MIN);
void recoverTree(TreeNode* root) {
inorder(root);
first->val = first->val + second->val;
second->val = first->val - second->val;
first->val = first->val - second->val;
}
void inorder(TreeNode* root) {
if (!root) return ;
inorder(root->left);
if (first==NULL && prev->val >= root->val) first = prev;
if (first!=NULL && prev->val >= root->val) second = root;
prev = root;
inorder(root->right);
}