void inOrder(TreeNode *root, vector<int> &v) {
if
(!root
) return
;
inOrder(root->left, v
);
v.emplace_
back(root->val
);
inOrder(root->right, v
);
}
bool twoSumBSTs(TreeNode *root1, TreeNode *root2, int target) {
vector<int> v1, v2
;
inOrder(root1, v1
);
inOrder(root2, v2
);
int l = 0, r = v2.
size() - 1
;
while (l < v1.size() && r >= 0) {
int val = v1[l] + v2[r]
;
if
(val > target
)
r--
;
else if
(val < target
)
l++
;
else
return true
;
}
return false
;
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-43165.html