Skip to main content
LeetCode 1214, Medium. Topics: Stack, Tree, Depth-First Search, Binary Search Tree, Two Pointers, Binary Search, Binary Tree. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 18 parametrized pytest cases, and a playground notebook:

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

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

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026