Given a binary search tree, and an integer **X**, the task is to find if there exists a triplet with sum **X**. Print **Yes** or **No** correspondingly. **Note** that the three nodes may not necessarily be distinct.

**Examples:**

Input:X = 15 5 / \ 3 7 / \ / \ 2 4 6 8Output:Yes {5, 5, 5} is one such triplet. {3, 5, 7}, {2, 5, 8}, {4, 5, 6} are some others.Input:X = 16 1 \ 2 \ 3 \ 4 \ 5Output:No

**Simple Approach:** A simple approach will be to convert the BST to a sorted array and then find the triplet using three pointers. This will take O(N) extra space where N is the number of nodes present in the Binary Search Tree. We have already discussed a similar problem in this article which takes O(N) extra space.

**Better approach:** We will solve this problem using a space efficient method by reducing the additional space complexity to O(H) where H is the height of BST. For that, we will use two pointer technique on BST.

We will traverse all the nodes for the tree one by one and for each node, we will try to find a pair with a sum equal to (X – curr->data) where ‘curr’ is the current node of the BST we are traversing.

We will use a technique similar to the technique discussed in this article for finding a pair.

**Algorithm:** Traverse each node of BST one by one and for each node:

- Create a forward and backward iterator for BST. Lets 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, thus we will increase the count by 1.
- If v1 + v2 less than or equal to x, we will make forward iterator point to the next element.
- If v1 + v2 greater than x, we will make backward iterator point to the previous element.

- We will continue the above while the left iterator doesn’t point to a node with larger value than right node.

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 that returns true if a pair exists ` `// in the binary search tree with sum equal to x ` `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.size() and it2.size()) { ` ` ` ` ` `// Variables to store values at ` ` ` `// it1 and it2 ` ` ` `int` `v1 = it1.top()->data, v2 = it2.top()->data; ` ` ` ` ` `// Base case ` ` ` `if` `(v1 + v2 == x) ` ` ` `return` `1; ` ` ` ` ` `if` `(v1 > v2) ` ` ` `break` `; ` ` ` ` ` `// 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` `0; ` `} ` ` ` `// Function that returns true if a triplet exists ` `// in the binary search tree with sum equal to x ` `bool` `existsTriplet(node* root, node* curr, ` `int` `x) ` `{ ` ` ` `// If current node is NULL ` ` ` `if` `(curr == NULL) ` ` ` `return` `0; ` ` ` ` ` `// Conditions for existence of a triplet ` ` ` `return` `(existsPair(root, x - curr->data) ` ` ` `|| existsTriplet(root, curr->left, x) ` ` ` `|| existsTriplet(root, curr->right, x)); ` `} ` ` ` `// 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 = 24; ` ` ` ` ` `if` `(existsTriplet(root, root, x)) ` ` ` `cout << ` `"Yes"` `; ` ` ` `else` ` ` `cout << ` `"No"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Yes

**Time complexity:** O(N^{2})

**Space complexity:** O(H)

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.

## Recommended Posts:

- Check whether there exists a triplet (i, j, k) such that arr[i] < arr[k] < arr[j] for i < j < k
- Check if a triplet with given sum exists in BST
- Find if there is a triplet in a Balanced BST that adds to zero
- Find distinct integers for a triplet with given product
- Find triplet such that number of nodes connecting these triplets is maximum
- Absolute distinct count in a Linked List
- Lexicographically largest possible String after removal of K characters
- Minimum number of Edges to be added to a Graph to satisfy the given condition
- Count of all possible Paths in a Tree such that Node X does not appear before Node Y
- Mix Order Traversal of a Binary Tree
- Count of Array elements greater than all elements on its left and next K elements on its right
- Mimimum number of leaves required to be removed from a Tree to satisfy the given condition
- Farthest distance of a Node from each Node of a Tree
- Sum of Nodes and respective Neighbors on the path from root to a vertex V

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.