-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecover_Binary_search_tree.cpp
More file actions
40 lines (40 loc) · 942 Bytes
/
Recover_Binary_search_tree.cpp
File metadata and controls
40 lines (40 loc) · 942 Bytes
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
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
vector<int> Solution::recoverTree(TreeNode* A) {
stack<TreeNode*> inorder;
TreeNode* curr = A;
vector<int> res;
while(curr || !inorder.empty()){
while(curr){
inorder.push(curr);
curr = curr->left;
}
curr = inorder.top();
inorder.pop();
res.push_back(curr->val);
curr = curr->right;
}
int i=0,j=res.size()-1;
vector<int> ans(2,-1);
int sm = -1, bg = -1;
while(i<res.size()-1 && j>0 ){
if(res[i+1]-res[i]<0 && bg==-1)
bg = i;
if(res[j]-res[j-1]<0 && sm==-1)
sm = j;
if(bg!=-1 && sm!=-1)
break;
i++;
j--;
}
ans[0] = res[sm];
ans[1] = res[bg];
return ans;
}