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).
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Pair with given product | Set 1 (Find if any pair exists)
- Find a pair with given sum in a Balanced BST
- Find a pair with the given difference
- Find pair with greatest product in array
- Find pairs with given sum such that elements of pair are in different rows
- Find pairs with given sum such that pair elements lie in different BSTs
- Find a pair with maximum product in array of Integers
- Find a pair of elements swapping which makes sum of two arrays same
- 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
- 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
- Count of elements which cannot form any pair whose sum is power of 2
- Check if a pair with given product exists in a Matrix
- Count of Palindromic Strings possible by swapping of a pair of Characters
- Print all possible palindromic string formed using any pair of given strings
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.