Problem statement

https://leetcode.com/problems/product-price-at-a-given-date/

Solution

Just create lists from our BST and then apply 2sum.

Complexity

Time complexity is O(n + m), where n and m are number of nodes in trees. Space complexity is O(n + m) as well.

Code

class Solution:
    def twoSumBSTs(self, root1, root2, target):  
        def dfs(node, ans):
            if not node: return
            dfs(node.left, ans)
            ans += [node.val]
            dfs(node.right, ans)
            
        l1, l2 = [], []
        dfs(root1, l1)
        dfs(root2, l2)
        l1, l2 = set(l1), set(l2)
        for el in l1:
            if target - el in l2: return True
            
        return False