Talentd
FleetCode

1214. Two Sum BSTs

Medium68.2% acceptance1 companyPremium elsewhere, free here

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.

Example 1:

Input: root1 = [2,1,4], root2 = [1,0,3], target = 5
Output: true
Explanation: 2 and 3 sum up to 5.

Example 2:

Input: root1 = [0,-10,10], root2 = [5,1,7,0,2], target = 18
Output: false

Constraints:

  • The number of nodes in each tree is in the range [1, 5000].
  • -109 <= Node.val, target <= 109

Asked at 1 company

Hints open one at a time

Hint 1
How can you reduce this problem to the classical Two Sum problem?
Hint 2
Do an in-order traversal of each tree to convert them to sorted arrays.
Hint 3
Solve the classical Two Sum problem.
4:54
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
[2,1,4]
[1,0,3]
5
Input
[0,-10,10]
[5,1,7,0,2]
18
Your code is saved on this device.Solution