Find a pair with given sum in BST in C++

In this tutorial, we are going to write a program that finds the pair whose sum is equal to the given number in the binary search tree.

We are going to store and values of trees in two different lists to find the pairs. Let's see the steps to solve the problem.

  • Create a struct node for a binary tree.

  • Write a function to insert a new node into the binary search tree.

    • Remember, in the binary search tree all the elements that are less than root are smaller, and right are larger.

  • Initialize two empty lists to store the left and right nodes of the tree.

  • Iterate over the binary search tree until left or right nodes are NULL or both the lists are not empty.

    • Write a loop to store all the elements into the left nodes list.

    • Write a loop to store all the elements into the right nodes list.

    • Get the last nodes from each list.

    • Check the two values.

      • If the left side node value is greater than or equal to the right side node value, then break the loop.

      • If the two values sum is equal to the given number, then print and break the loop.

      • If the two values sum is less than the given number, then delete the last node from the left list and move to the right of the node.

      • If the two values sum is greater than the given number, then delete the last node from the right list and move to the left of the node.


Let's see the code.

 Live Demo

using namespace std;
struct Node{
   int data;
   Node *left, *right, *root;
   Node(int data) {
      this->data = data;
      left = NULL;
      right = NULL;
      root = NULL;
Node* insertNewNode(Node *root, int data){
   if (root == NULL) {
      root = new Node(data);
      return root;
   if (root->data < data) {
      root->right = insertNewNode(root->right, data);
   else if (root->data > data) {
      root->left = insertNewNode(root->left, data);
   return root;
void findThePairs(Node *node, int target) {
   vector<Node*> left_side_nodes;
   vector<Node*> right_side_nodes;
   Node *current_left = node;
   Node *current_right = node;
   while (current_left != NULL || current_right != NULL || (left_side_nodes.size() > 0 && right_side_nodes.size() > 0)) {
      while (current_left != NULL) {
         current_left = current_left->left;
      while (current_right != NULL) {
         current_right = current_right->right;
      Node *left_side_node = left_side_nodes[left_side_nodes.size() - 1];
      Node *right_side_node = right_side_nodes[right_side_nodes.size() - 1];
      int left_side_value = left_side_node->data;
      int right_side_value = right_side_node->data;
      if (left_side_value >= right_side_value) {
      if (left_side_value + right_side_value < target) {
         current_left = left_side_node->right;
      else if (left_side_value + right_side_value > target) {
         current_right = right_side_node->left;
      else {
         cout << left_side_node->data << " " << right_side_node->data << endl;
int main() {
   Node *root = NULL;
   root = insertNewNode(root, 25);
   root = insertNewNode(root, 20);
   root = insertNewNode(root, 30);
   root = insertNewNode(root, 15);
   root = insertNewNode(root, 21);
   root = insertNewNode(root, 19);
   root = insertNewNode(root, 31);
   findThePairs(root, 36);


If you execute the above program, you will get the following result.

15 21


If you have any queries in the tutorial, mention them in the comment section.

Updated on: 01-Feb-2021


Kickstart Your Career

Get certified by completing the course

Get Started