> ## 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 IV - Input is a BST Python Solution

> Tested Python solution for LeetCode 653 with 28 pytest cases. Generate a practice environment with lcpy.

LeetCode 653, [Easy](/catalog/easy). Topics: [Hash Table](/catalog/topics/hash-table), [Two Pointers](/catalog/topics/two-pointers), [Tree](/catalog/topics/tree), [Depth-First Search](/catalog/topics/depth-first-search), [Breadth-First Search](/catalog/topics/breadth-first-search), [Binary Search Tree](/catalog/topics/binary-search-tree), [Binary Tree](/catalog/topics/binary-tree). [View on LeetCode](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/description/).

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

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

## Problem

\<p>Given the \<code>root\</code> of a binary search tree and an integer \<code>k\</code>, return \<code>true\</code> \<em>if there exist two elements in the BST such that their sum is equal to\</em> \<code>k\</code>, \<em>or\</em> \<code>false\</code> \<em>otherwise\</em>.\</p>

### Examples

![Example 1](https://assets.leetcode.com/uploads/2020/09/21/sum_tree_1.jpg)

```
Input: root = [5,3,6,2,4,null,7], k = 9
Output: true
```

![Example 2](https://assets.leetcode.com/uploads/2020/09/21/sum_tree_2.jpg)

```
Input: root = [5,3,6,2,4,null,7], k = 28
Output: false
```

### Constraints

\<ul>
\<li>The number of nodes in the tree is in the range \<code>\[1, 10^4]\</code>.\</li>
\<li>\<code>-10^4 \<= Node.val \<= 10^4\</code>\</li>
\<li>\<code>root\</code> is guaranteed to be a \<strong>valid\</strong> binary search tree.\</li>
\<li>\<code>-10^5 \<= k \<= 10^5\</code>\</li>
\</ul>

## Solution

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

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


class Solution:
    # Time: O(n)
    # Space: O(n)
    def find_target(self, root: TreeNode[int] | None, k: int) -> bool:
        seen: set[int] = set()

        def dfs(node: TreeNode[int] | None) -> bool:
            if node is None:
                return False
            if k - node.val in seen:
                return True
            seen.add(node.val)
            return dfs(node.left) or dfs(node.right)

        return dfs(root)
```

## Complexity

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

## Tags


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