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.
- Find a pair with given sum in BST
- Print Common Nodes in Two Binary Search Trees
- Add all greater values to every node in a given BST
- Remove BST keys outside the given range
- Find if there is a triplet in a Balanced BST that adds to zero
- Floor and Ceil from a BST
- Two nodes of a BST are swapped, correct the BST
- Binary Tree to Binary Search Tree Conversion
- Merge two BSTs with limited extra space
- Merge Two Balanced Binary Search Trees
- Sorted Linked List to Balanced BST
- Find k-th smallest element in BST (Order Statistics in BST)
- Inorder Successor in Binary Search Tree
- Total number of possible Binary Search Trees and Binary Trees with n keys
- A program to check if a binary tree is BST or not