# Pair with a given sum in BST | Set 2

Given a binary search tree, and an integer **X**, the task is to check if there exists a pair of distinct nodes in BST with sum equal to **X**. If yes then print **Yes** else print **No**.

**Examples:**

Input:X = 5 5 / \ 3 7 / \ / \ 2 4 6 8Output:Yes 2 + 3 = 5. Thus, the answer is "Yes"Input:X = 10 1 \ 2 \ 3 \ 4 \ 5Output:No

**Approach:** We have already discussed a hash based approach in this article. The space complexity of this is O(N) where N is the number of nodes in BST.

In this article, we will solve the same problem using a space efficient method by reducing the space complexity to O(H) where H is the height of BST. For that, we will use two pointer technique on BST. Thus, we will maintain a forward and a backward iterator that will iterate the BST in the order of in-order and reverse in-order traversal respectively. Following are the steps to solve the problem:

- Create a forward and backward iterator for BST. Let’s say the value of nodes they are pointing at are v1 and v2.
- Now at each step,
- If v1 + v2 = X, we found a pair.
- If v1 + v2 < x, we will make forward iterator point to the next element.
- If v1 + v2 > x, we will make backward iterator point to the previous element.

- If we find no such pair, answer will be “No”.

Below is the implementation of the above approach:

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Node of the 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 with given sum ` `bool` `existsPair(node* root, ` `int` `x) ` `{ ` ` ` `// Iterators for BST ` ` ` `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; ` ` ` ` ` `// Two pointer technique ` ` ` `while` `(it1.top() != it2.top()) { ` ` ` ` ` `// Variables to store values at ` ` ` `// it1 and it2 ` ` ` `int` `v1 = it1.top()->data, v2 = it2.top()->data; ` ` ` ` ` `// Base case ` ` ` `if` `(v1 + v2 == x) ` ` ` `return` `true` `; ` ` ` ` ` `// Moving forward pointer ` ` ` `if` `(v1 + v2 < x) { ` ` ` `c = it1.top()->right; ` ` ` `it1.pop(); ` ` ` `while` `(c != NULL) ` ` ` `it1.push(c), c = c->left; ` ` ` `} ` ` ` ` ` `// Moving backward pointer ` ` ` `else` `{ ` ` ` `c = it2.top()->left; ` ` ` `it2.pop(); ` ` ` `while` `(c != NULL) ` ` ` `it2.push(c), c = c->right; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Case when no pair is found ` ` ` `return` `false` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `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 = 5; ` ` ` ` ` `// Calling required function ` ` ` `if` `(existsPair(root, x)) ` ` ` `cout << ` `"Yes"` `; ` ` ` `else` ` ` `cout << ` `"No"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Yes

## Recommended Posts:

- Find a pair with given sum in BST
- Find a pair with given sum in a Balanced BST
- Pair with minimum absolute difference | BST
- Longest path between any pair of vertices
- Number of pair of positions in matrix which are not accessible
- Find pairs with given sum such that pair elements lie in different BSTs
- Find the number of pair of Ideal nodes in a given tree
- Find the shortest distance between any pair of two different good nodes
- Pair with minimum absolute difference after solving each query
- Number of distinct pair of edges such that it partitions both trees into same subsets of nodes
- Find if there is a pair in root to a leaf path with sum equals to root's data
- Insertion in n-ary tree in given order and Level order traversal
- Red-Black Trees | Top-Down Insertion
- m-WAY Search Trees | Set-1 ( Searching )

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.