原题题目
代码实现(首刷自解)
class Solution {
public:void visit(vector<int>& v1,TreeNode* root){
if(!root) return;visit(v1,root->left);v1.emplace_back(root->val);visit(v1,root->right);}bool twoSumBSTs(TreeNode* root1, TreeNode* root2, int target) {
vector<int> v1,v2;visit(v1,root1);visit(v2,root2);int left = 0,pos = v2.size();while(left < v1.size()){
auto iter = lower_bound(v2.begin(),v2.begin()+pos,target-v1[left]);++left;if(iter == v2.end()) continue;if(*iter == target-v1[left-1]) return true;pos = iter - v2.begin();}return false;}
};