
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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.
Example
Let's see the code.
#include<bits/stdc++.h> 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) { left_side_nodes.push_back(current_left); current_left = current_left->left; } while (current_right != NULL) { right_side_nodes.push_back(current_right); 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) { break; } if (left_side_value + right_side_value < target) { left_side_nodes.pop_back(); current_left = left_side_node->right; } else if (left_side_value + right_side_value > target) { right_side_nodes.pop_back(); current_right = right_side_node->left; } else { cout << left_side_node->data << " " << right_side_node->data << endl; break; } } } 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); }
Output
If you execute the above program, you will get the following result.
15 21
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- Find a pair with given sum in a Balanced BST in C++
- Find a pair with given sum in a Balanced BST in Java
- Find all the pairs with given sum in a BST in C++
- Find the Pair with Given Sum in a Matrix using C++
- Check if a triplet with given sum exists in BST in Python
- Find a pair with the given difference in C++
- Find pairs with given sum such that pair elements lie in different BSTs in Python
- Find required sum pair with JavaScript
- Program to find out the largest sum value of a BST in a given binary tree in Python
- Find pairs with given sum such that elements of pair are in different rows in Python
- Find the Pair with a Maximum Sum in a Matrix using C++
- Find Sum of pair from two arrays with maximum sum in C++
- Find pair for given sum in a sorted singly linked without extra space in C++
- Find any pair with given GCD and LCM in C++
- JavaScript Program to Find a pair with the given difference
