-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path653.txt
55 lines (40 loc) · 1.13 KB
/
653.txt
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
48
49
50
51
52
53
54
55
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool findTarget(TreeNode* root, int k) {
vector <int> a;
mid_order(root,a);
int left=0;
int right=a.size()-1;
while (left<right){
if (a[left]+a[right]==k) return true;
if (a[left]+a[right]<k) left++;
if (a[left]+a[right]>k) right--;
}
return false;
}
private:
void mid_order(TreeNode* root, vector<int>&a){
if(root==nullptr) return ;
mid_order(root->left, a);
a.push_back(root->val);
mid_order(root->right, a);
}
};