Given a BST and a sum, find if there is a pair with given sum.
We have discussed different approaches to find a pair with given sum in below post.Find a pair with given sum in a Balanced BST
In this post, hashing based solution is discussed. We traverse binary search tree by inorder way and insert node’s value into a set. Also check for any node, difference between given sum and node’s value in set, if it is found then pair exists otherwise it doesn’t exist.
Pair is found (8, 25)
Time Complexity is O(n).
- Pair with given product | Set 1 (Find if any pair exists)
- Find a pair with the given difference
- Find a pair with given sum in a Balanced BST
- Find pairs with given sum such that pair elements lie in different BSTs
- Find pairs with given sum such that elements of pair are in different rows
- Find pair with greatest product in array
- Find a pair of elements swapping which makes sum of two arrays same
- Find a pair with maximum product in array of Integers
- Find if there is a pair in root to a leaf path with sum equals to root's data
- Pair with a given sum in BST | Set 2
- Pair with given sum in matrix
- Linked List Pair Sum
- Subarray with no pair sum divisible by K
- Given an array A and a number x, check for pair in A with sum as x
- Pair with minimum absolute difference | BST
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Vikash Kumar 37