Two Sum in BST
An easy BST problem included in Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.
- Topic
- BST
- Sheets
- 1
- Core for
- 2 roles
- Platform
- LeetCode
The problem
Given the root of a binary search tree and an integer target, return true if there exist two elements in the BST such that their sum equals the target. Each element can only be used once.
Example 1
- Input
- root=[5,3,6,2,4,null,7], k=9
- Output
- true
- Why
- Elements 3+6=9 or 2+7=9 or 4+5=9, so a valid pair exists.
Example 2
- Input
- root=[5,3,6,2,4,null,7], k=28
- Output
- false
- Why
- No pair of elements sums to 28.
Example 3
- Input
- root=[2,1,3], k=4
- Output
- true
- Why
- Elements 1+3=4.
Constraints
- The number of nodes is in the range [1, 104].
- -105 <= Node.val <= 105
- -109 <= k <= 109
How to think about it
Updated 2026-09-09Two-sum on an array uses two pointers converging from opposite ends once elements are sorted. A BST is already sorted: an ascending iterator walking forward from the minimum and a descending iterator walking backward from the maximum simulate the exact same two-pointer squeeze directly on the tree.
Approaches, worst first
Hash set traversal
time O(n) · space O(n)
Visit every node using any standard traversal, checking whether `k - node.val` is in a hash set before inserting `node.val`. Completely ignores the BST ordering, taking full linear memory regardless of tree height.
Inorder array two-pointer
time O(n) · space O(n)
Dump the inorder traversal into an array to obtain a sorted list, then pinch inward with left and right pointers. Clean and optimal in time, but buffers all n elements upfront.
Two BST iteratorsWrite this one
time O(n) · space O(h)
Instantiate two stack-based iterators: one moving forward (left-root-right) and one moving backward (right-root-left). Advance the forward pointer when the pair sum is too small and the backward pointer when too large, halting when they meet.
Where people lose marks · 2
- Reusing a single node. When `2 * node.val == k`, an unchecked hash set or pointer logic can pair a node with itself if the presence check happens after insertion.
- Single-node tree edge case: a BST with only one node can never form a pair; the loop must terminate safely and return false.
The theory behind it
BST — the ground this problem stands on. All BST problems
What BST is
A binary search tree is a binary tree that enforces a strict ordering rule at every node. Every value stored in a node's left branch must be smaller than the node itself, and every value in its right branch must be larger. Because this rule holds true everywhere down the tree, searching for a value does not require checking every branch. At each step, a single comparison lets you discard an entire half of the remaining nodes.
When to reach for it
Reach for a binary search tree when problems mention sorted tree structures, finding the kth smallest element, checking tree validity, or searching within a range of values. Signals include queries for the next greater element in a dynamic set, finding the lowest common ancestor in a sorted tree, or converting sorted arrays into height-balanced trees. When data must remain sorted while supporting continuous insertions and lookups, BST properties are the target.
How the pattern works
Use the ordering property to prune entire subtrees. When looking for a key, move left if the target is smaller than the current node, or move right if it is larger, stopping when values match or when reaching a null link. For validating a tree, do not merely compare a node to its immediate children; pass valid lower and upper value bounds down through recursive calls. Walking a valid binary search tree in-order visits all values in strictly increasing numerical order.
What each operation costs
| Operation | Time |
|---|---|
| search, insert, or delete on balanced tree | O(log n) |
| search, insert, or delete on degenerate line tree | O(n) |
| in-order traversal visiting all nodes in sorted order | O(n) |
What usually goes wrong with BST
- Validating a tree by checking only immediate children against the parent, missing violations where a node deep in the left subtree is larger than an ancestor higher up.
- Allowing duplicate values on the wrong side when the problem specification requires strictly smaller values on the left and strictly greater on the right.
- Deleting a node with two children by removing it directly instead of replacing its value with its in-order predecessor or successor and deleting that leaf instead.
Which roles need this problem
BST is a core topic for these 2 roles — if you're targeting one of them, this problem is early in your path, not optional.
Secondary for 6 more roles, including Full-Stack Developer, Android Developer, iOS Developer.
Track this in your role's order
Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.
Start freeMore BST problems
Problem set and role mapping as of .