-
Notifications
You must be signed in to change notification settings - Fork 0
/
653.cpp
33 lines (32 loc) · 858 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
/**
* 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:
void helper(vector<int> &l, TreeNode* root)
{
if(root->left != NULL) helper(l, root->left);
l.push_back(root->val);
if(root->right != NULL) helper(l, root->right);
}
bool findTarget(TreeNode* root, int k) {
if(root == NULL) return false;
vector<int> sort_list;
helper(sort_list, root);
int len = sort_list.size();
int f = 0, s = len - 1;
while(f < s)
{
if(sort_list[f] + sort_list[s] == k) return true;
else if (sort_list[f] + sort_list[s] > k) s--;
else f++;
}
return false;
}
};