Skip to main content
LeetCode 653, Easy. Topics: Hash Table, Two Pointers, Tree, Depth-First Search, Breadth-First Search, Binary Search Tree, Binary Tree. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 28 parametrized pytest cases, and a playground notebook:

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
Example 2

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, full suite in test_solution.py:

Complexity

Tags

Last modified on September 7, 2026