> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Two Sum BSTs Python Solution with Tests

> Tested Python solution for LeetCode 1214 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 1214, [Medium](/catalog/medium). Topics: [Stack](/catalog/topics/stack), [Tree](/catalog/topics/tree), [Depth-First Search](/catalog/topics/depth-first-search), [Binary Search Tree](/catalog/topics/binary-search-tree), [Two Pointers](/catalog/topics/two-pointers), [Binary Search](/catalog/topics/binary-search), [Binary Tree](/catalog/topics/binary-tree). [View on LeetCode](https://leetcode.com/problems/two-sum-bsts/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1214   # by problem number
lcpy gen -s two_sum_bsts   # by problem name
```

## Problem

\<p>Given the roots of two binary search trees, \<code>root1\</code> and \<code>root2\</code>, return \<code>true\</code> 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 \<code>target\</code>.\</p>

### Examples

![Example 1](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1200-1299/1214.Two%20Sum%20BSTs/images/ex1.png)

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

![Example 2](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1200-1299/1214.Two%20Sum%20BSTs/images/ex2.png)

```
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].
* -10^9 \<= Node.val, target \<= 10^9.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/two_sum_bsts/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/two_sum_bsts/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from leetcode_py import TreeNode


class Solution:
    # Time: O(m + n)
    # Space: O(m + n)
    def two_sum_bsts(
        self, root1: TreeNode[int] | None, root2: TreeNode[int] | None, target: int
    ) -> bool:
        def inorder(root: TreeNode[int] | None, out: list[int]) -> None:
            if root is None:
                return
            inorder(root.left, out)
            out.append(root.val)
            inorder(root.right, out)

        nums1: list[int] = []
        nums2: list[int] = []
        inorder(root1, nums1)
        inorder(root2, nums2)
        i, j = 0, len(nums2) - 1
        while i < len(nums1) and j >= 0:
            total = nums1[i] + nums2[j]
            if total == target:
                return True
            if total < target:
                i += 1
            else:
                j -= 1
        return False
```

## Complexity

| Time | Space |
| - | - |
| O(m + n) | O(m + n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.