Given the roots of two binary search trees, root1 and root2, return true if and only if there is a node in the first tree and a node in the second tree whose values sum up to a given integer target.

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def twoSumBSTs(self, root1: Optional[TreeNode], root2: Optional[TreeNode], target: int) -> bool:
def transform(node: Optional[TreeNode], arr: List[int]):
if node:
transform(node.left, arr)
arr.append(node.val)
transform(node.right, arr)
a, b = [], []
transform(root1, a)
transform(root2, b)
left = 0
right = len(b) - 1
while left < len(a) and right >= 0:
if a[left] + b[right] < target:
left += 1
elif a[left] + b[right] > target:
right -= 1
else:
return True
return False
해당 문제는 순서대로 작은 태스크를 해결하면 된다.