class Solution {
public:
void dfs(TreeNode* now,map<int,int>& mp){
mp[now->val] = 1;
if(now->left != NULL) dfs(now->left,mp);
if(now->right != NULL) dfs(now->right,mp);
}
bool dfsB(TreeNode* now,int& target,map<int,int>& mp){
if(mp[target-now->val] == 1) return true;
bool ans = false;
if(now->left != NULL) ans = ans || dfsB(now->left,target,mp);
if(now->right != NULL) ans = ans || dfsB(now->right,target,mp);
return ans;
}
bool twoSumBSTs(TreeNode* root1, TreeNode* root2, int target) {
if(root1 == NULL) return false;
if(root2 == NULL) return false;
map<int,int> mp;
dfs(root1,mp);
return dfsB(root2,target,mp);
}
};