Given a Balanced Binary Search Tree and a target sum, write a function that returns true if there is a pair with sum equals to target sum, otherwise return false. Expected time complexity is O(n) and only O(Logn) extra space can be used. Any modification to Binary Search Tree is not allowed. Note that height of a Balanced BST is always O(Logn).
This problem is mainly extension of the previous post. Here we are not allowed to modify the BST.
The Brute Force Solution is to consider each pair in BST and check whether the sum equals to X. The time complexity of this solution will be O(n^2).
A Better Solution is to create an auxiliary array and store Inorder traversal of BST in the array. The array will be sorted as Inorder traversal of BST always produces sorted data. Once we have the Inorder traversal, we can pair in O(n) time (See this for details). This solution works in O(n) time, but requires O(n) auxiliary space.
Pair Found: 8 + 25 = 33
A space optimized solution is discussed in previous post. The idea was to first in-place convert BST to Doubly Linked List (DLL), then find pair in sorted DLL in O(n) time. This solution takes O(n) time and O(Logn) extra space, but it modifies the given BST.
The solution discussed below takes O(n) time, O(Logn) space and doesn’t modify BST. The idea is same as finding the pair in sorted array (See method 1 of this for details). We traverse BST in Normal Inorder and Reverse Inorder simultaneously. In reverse inorder, we start from the rightmost node which is the maximum value node. In normal inorder, we start from the left most node which is minimum value node. We add sum of current nodes in both traversals and compare this sum with given target sum. If the sum is same as target sum, we return true. If the sum is more than target sum, we move to next node in reverse inorder traversal, otherwise we move to next node in normal inorder traversal. If any of the traversals is finished without finding a pair, we return false. Following is C++ implementation of this approach.
Pair Found: 8 + 25 = 33
This article is compiled by Kumar and reviewed by GeeksforGeeks team. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Pair with given product | Set 1 (Find if any pair exists)
- Find if there is a triplet in a Balanced BST that adds to zero
- Find a pair with given sum in BST
- Find a pair with the given difference
- Find pairs with given sum such that pair elements lie in different BSTs
- Find a pair of elements swapping which makes sum of two arrays same
- Find a pair with maximum product in array of Integers
- Convert a normal BST to Balanced BST
- Sorted Array to Balanced BST
- Sorted Linked List to Balanced BST
- Check for balanced parenthesis without using stack
- Check for balanced parentheses in an expression
- How to determine if a binary tree is height-balanced?
- Merge Two Balanced Binary Search Trees
- Minimum number of bracket reversals needed to make an expression balanced