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).
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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
- Subarray with no pair sum divisible by K
- Linked List Pair Sum
- Pair with given sum and maximum shortest distance from end
- Pair with minimum absolute difference | BST
- Given an array A and a number x, check for pair in A with sum as x
- Check if a pair with given product exists in a Matrix
- Count of elements which cannot form any pair whose sum is power of 2
- Pair of fibonacci numbers with a given sum and minimum absolute difference
- Longest subsequence such that absolute difference between every pair is atmost 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.