Two sum bsts
January 1, 2023
treeProblem URL: Two sum bsts
We will traverse the tree and store the values in a set. Then we will iterate over the set and check if target - num
is in the set.
# 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 dfs(node):
if not node:
return []
return dfs(node.left) + dfs(node.right) + [node.val]
q1 = set(dfs(root1))
return any(target - a in q1 for a in dfs(root2))
Time complexity: O(n)
Space complexity: O(n)