-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path653.cpp
40 lines (37 loc) · 837 Bytes
/
653.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
class Solution {
public:
bool findTarget(TreeNode* root, int k) {
if(root==NULL){
return 1;
}
vector<int>A;
traversal(root,A);
int n=A.size();
int a=A[0];
int b=A[n-1];
int i=1;
int j=1;
while(a<b){
if((a+b)==k){
return 1;
}
else if((a+b)>k){
b=A[n-j];
j++;
}
else if((a+b)<k){
a=A[i];
i++;
}
}
return 0;
}
void traversal(TreeNode * root,vector<int>&A){
if(root==NULL){
return ;
}
if(root->left) traversal(root->left,A);
A.push_back(root->val);
if(root->right) traversal(root->right,A);
}
};