Number of pairs with a given sum in a Binary Search Tree

Given a Binary Search Tree, and a number X. The task is to find the number of distinct pairs of distinct nodes in BST with a sum equal to X. No two nodes have the same values.

Examples:


Input : X = 5
          5 
        /   \ 
       3     7 
      / \   / \ 
     2   4 6   8    
Output : 1
{2, 3} is the only possible pair. 
Thus, the answer is equal to 1.

Input : X = 6
      1
       \
        2
         \
          3
           \
            4
             \
              5
   
Output : 2
Possible pairs are {{1, 5}, {2, 4}}.

Naive Approach: The idea is to hash all the elements of BST or convert the BST to a sorted array. After that find the number of pairs using the algorithm given here.
Time Complexity: O(N).
Space Complexity: O(N).

Space Optimized Approach : The idea is to use two pointer technique on BST. Maintain forward and backward iterators that will iterate the BST in the order of in-order and reverse in-order traversal respectively.

  1. Create forward and backward iterators for BST. Let’s say that the value of nodes they are pointing at are equal to v1 and v2 respectively.
  2. Now at each step,
    • If v1 + v2 = X, the pair is found, thus increase the count by 1.
    • If v1 + v2 is less than or equal to x, we will make forward iterator point to the next element.
    • If v1 + v2 is greater than x, we will make backward iterator point to the previous element.
  3. Repeat the above steps untill both iterators don’t point to the same node.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Node of Binary tree
struct node {
    int data;
    node* left;
    node* right;
    node(int data)
    {
        this->data = data;
        left = NULL;
        right = NULL;
    }
};
  
// Function to find a pair
int cntPairs(node* root, int x)
{
    // Stack to store nodes for
    // forward and backward iterator
    stack<node *> it1, it2;
  
    // Initializing forward iterator
    node* c = root;
    while (c != NULL)
        it1.push(c), c = c->left;
  
    // Initializing backward iterator
    c = root;
    while (c != NULL)
        it2.push(c), c = c->right;
  
    // Variable to store final answer
    int ans = 0;
  
    // two pointer technique
    while (it1.top() != it2.top()) {
  
        // Variables to store the
        // value of the nodes current
        // iterators are pointing to.
        int v1 = it1.top()->data;
        int v2 = it2.top()->data;
  
        // If we find a pair
        // then count is increased by 1
        if (v1 + v2 == x)
            ans++;
  
        // Moving forward iterator
        if (v1 + v2 <= x) {
            c = it1.top()->right;
            it1.pop();
            while (c != NULL)
                it1.push(c), c = c->left;
        }
  
        // Moving backward iterator
        else {
            c = it2.top()->left;
            it2.pop();
            while (c != NULL)
                it2.push(c), c = c->right;
        }
    }
  
    // Returning final answer
    return ans;
}
  
// Driver code
int main()
{
    /*    5 
        /   \ 
       3     7 
      / \   / \ 
     2   4 6   8 */
    node* root = new node(5);
    root->left = new node(3);
    root->right = new node(7);
    root->left->left = new node(2);
    root->left->right = new node(4);
    root->right->left = new node(6);
    root->right->right = new node(8);
  
    int x = 10;
  
    cout << cntPairs(root, x);
  
    return 0;
}

chevron_right


Output:

3

Time complexity: O(N)
Space complexity: O(H) where H is the height of BST



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.