要求:rt
思路:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> v;
void dfs(TreeNode* root){
if(!root)return;
dfs(root->left);
v.push_back(root->val);
dfs(root->right);
}
bool findTarget(TreeNode* root, int k) {
dfs(root);
int left=0,right=v.size()-1;
while(left<right){
int sum=v[left]+v[right];
if(sum==k)return true;
else if(sum<k)++left;
else --right;
}
return false;
}
};