# Find all the pairs with given sum in a BST | Set 2

Given a Binary Search Tree and an integer sum, the task is to find all the pairs from the tree whose sum is equal to the given integer sum.
We have discussed a similar problem in this post.

Examples:

```Input:
2
/   \
1     6
/ \
5   7
/
3
\
4
sum = 8
Output:
1 7
2 6
3 5

Input:
2
/   \
1     3
\
4
sum = 5
Output:
1 4
2 3
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Traverse the tree in pre-order manner from both the side, left and right and store the values of left and right side into the ArrayList LeftList and RightList respectively. On reaching the leaf node, take out the left side last value and right side last value from the respective ArrayLists. There will be three conditions:

1. left side value + right side value < sum: Delete the last value of LeftList and make the left side execution to the right side because on moving from left side to the right side in the tree the value of node increases.
2. left side value + right side value > sum: Delete the last value of RightList and make the right side execution to the left side because on moving from right side to the left side in the tree the value of node decreases.
3. left side value + right side value = sum: Delete the last value of both the lists and make the left side execution to right side and right side execution to the left side.

Below is the implementation of the above approach:

## Java

 `// Java implementation of the approach ` `import` `java.util.*; ` `public` `class` `GFG { ` ` `  `    ``// A binary tree node ` `    ``public` `static` `class` `Node { ` `        ``int` `data; ` `        ``Node left, right, root; ` ` `  `        ``Node(``int` `data) ` `        ``{ ` `            ``this``.data = data; ` `        ``} ` `    ``} ` ` `  `    ``// Function to add a node to the BST ` `    ``public` `static` `Node AddNode(Node root, ``int` `data) ` `    ``{ ` ` `  `        ``// If the tree is empty, return a new node ` `        ``if` `(root == ``null``) { ` `            ``root = ``new` `Node(data); ` `            ``return` `root; ` `        ``} ` ` `  `        ``// Otherwise, recur down the tree ` `        ``if` `(root.data < data) ` `            ``root.right = AddNode(root.right, data); ` ` `  `        ``else` `if` `(root.data > data) ` `            ``root.left = AddNode(root.left, data); ` ` `  `        ``return` `root; ` `    ``} ` ` `  `    ``// Function to find the target pairs ` `    ``public` `static` `void` `TargetPair(Node node, ``int` `tar) ` `    ``{ ` ` `  `        ``// LeftList which stores the left side values ` `        ``ArrayList LeftList = ``new` `ArrayList<>(); ` ` `  `        ``// RightList which stores the right side values ` `        ``ArrayList RightList = ``new` `ArrayList<>(); ` ` `  `        ``// curr_left pointer is used for left side execution and ` `        ``// curr_right pointer is used for right side execution ` `        ``Node curr_left = node; ` `        ``Node curr_right = node; ` ` `  `        ``while` `(curr_left != ``null` `|| curr_right != ``null` `               ``|| LeftList.size() > ``0` `&& RightList.size() > ``0``) { ` ` `  `            ``// Storing the left side values into LeftList ` `            ``// till leaf node not found ` `            ``while` `(curr_left != ``null``) { ` `                ``LeftList.add(curr_left); ` `                ``curr_left = curr_left.left; ` `            ``} ` ` `  `            ``// Storing the right side values into RightList ` `            ``// till leaf node not found ` `            ``while` `(curr_right != ``null``) { ` `                ``RightList.add(curr_right); ` `                ``curr_right = curr_right.right; ` `            ``} ` ` `  `            ``// Last node of LeftList ` `            ``Node LeftNode = LeftList.get(LeftList.size() - ``1``); ` ` `  `            ``// Last node of RightList ` `            ``Node RightNode = RightList.get(RightList.size() - ``1``); ` ` `  `            ``int` `leftVal = LeftNode.data; ` `            ``int` `rightVal = RightNode.data; ` ` `  `            ``// To prevent repetition like 2, 6 and 6, 2 ` `            ``if` `(leftVal >= rightVal) ` `                ``break``; ` ` `  `            ``// Delete the last value of LeftList and make ` `            ``// the execution to the right side ` `            ``if` `(leftVal + rightVal < tar) { ` `                ``LeftList.remove(LeftList.size() - ``1``); ` `                ``curr_left = LeftNode.right; ` `            ``} ` ` `  `            ``// Delete the last value of RightList and make ` `            ``// the execution to the left side ` `            ``else` `if` `(leftVal + rightVal > tar) { ` `                ``RightList.remove(RightList.size() - ``1``); ` `                ``curr_right = RightNode.left; ` `            ``} ` ` `  `            ``// (left value + right value) = target ` `            ``// then print the left value and right value ` `            ``// Delete the last value of left and right list ` `            ``// and make the left execution to right side ` `            ``// and right side execution to left side ` `            ``else` `{ ` `                ``System.out.println(LeftNode.data + ``" "` `+ RightNode.data); ` ` `  `                ``RightList.remove(RightList.size() - ``1``); ` `                ``LeftList.remove(LeftList.size() - ``1``); ` `                ``curr_left = LeftNode.right; ` `                ``curr_right = RightNode.left; ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] b) ` `    ``{ ` ` `  `        ``Node root = ``null``; ` `        ``root = AddNode(root, ``2``); ` `        ``root = AddNode(root, ``6``); ` `        ``root = AddNode(root, ``5``); ` `        ``root = AddNode(root, ``3``); ` `        ``root = AddNode(root, ``4``); ` `        ``root = AddNode(root, ``1``); ` `        ``root = AddNode(root, ``7``); ` `        ``int` `sum = ``8``; ` `        ``TargetPair(root, sum); ` `    ``} ` `} `

## C#

 `// C# program to implement ` `// the above approach ` `using` `System.Collections.Generic; ` `using` `System; ` ` `  `class` `GFG  ` `{ ` ` `  `    ``// A binary tree node ` `    ``public` `class` `Node ` `    ``{ ` `        ``public` `int` `data; ` `        ``public` `Node left, right, root; ` ` `  `        ``public` `Node(``int` `data) ` `        ``{ ` `            ``this``.data = data; ` `        ``} ` `    ``} ` ` `  `    ``// Function to add a node to the BST ` `    ``public` `static` `Node AddNode(Node root, ``int` `data) ` `    ``{ ` ` `  `        ``// If the tree is empty, return a new node ` `        ``if` `(root == ``null``)  ` `        ``{ ` `            ``root = ``new` `Node(data); ` `            ``return` `root; ` `        ``} ` ` `  `        ``// Otherwise, recur down the tree ` `        ``if` `(root.data < data) ` `            ``root.right = AddNode(root.right, data); ` ` `  `        ``else` `if` `(root.data > data) ` `            ``root.left = AddNode(root.left, data); ` ` `  `        ``return` `root; ` `    ``} ` ` `  `    ``// Function to find the target pairs ` `    ``public` `static` `void` `TargetPair(Node node, ``int` `tar) ` `    ``{ ` ` `  `        ``// LeftList which stores the left side values ` `        ``List LeftList = ``new` `List(); ` ` `  `        ``// RightList which stores the right side values ` `        ``List RightList = ``new` `List(); ` ` `  `        ``// curr_left pointer is used for left side execution and ` `        ``// curr_right pointer is used for right side execution ` `        ``Node curr_left = node; ` `        ``Node curr_right = node; ` ` `  `        ``while` `(curr_left != ``null` `|| curr_right != ``null` `            ``|| LeftList.Count > 0 && RightList.Count > 0)  ` `        ``{ ` ` `  `            ``// Storing the left side values into LeftList ` `            ``// till leaf node not found ` `            ``while` `(curr_left != ``null``)  ` `            ``{ ` `                ``LeftList.Add(curr_left); ` `                ``curr_left = curr_left.left; ` `            ``} ` ` `  `            ``// Storing the right side values into RightList ` `            ``// till leaf node not found ` `            ``while` `(curr_right != ``null``) ` `            ``{ ` `                ``RightList.Add(curr_right); ` `                ``curr_right = curr_right.right; ` `            ``} ` ` `  `            ``// Last node of LeftList ` `            ``Node LeftNode = LeftList[LeftList.Count - 1]; ` ` `  `            ``// Last node of RightList ` `            ``Node RightNode = RightList[RightList.Count - 1]; ` ` `  `            ``int` `leftVal = LeftNode.data; ` `            ``int` `rightVal = RightNode.data; ` ` `  `            ``// To prevent repetition like 2, 6 and 6, 2 ` `            ``if` `(leftVal >= rightVal) ` `                ``break``; ` ` `  `            ``// Delete the last value of LeftList and make ` `            ``// the execution to the right side ` `            ``if` `(leftVal + rightVal < tar)  ` `            ``{ ` `                ``LeftList.RemoveAt(LeftList.Count - 1); ` `                ``curr_left = LeftNode.right; ` `            ``} ` ` `  `            ``// Delete the last value of RightList and make ` `            ``// the execution to the left side ` `            ``else` `if` `(leftVal + rightVal > tar) ` `            ``{ ` `                ``RightList.RemoveAt(RightList.Count - 1); ` `                ``curr_right = RightNode.left; ` `            ``} ` ` `  `            ``// (left value + right value) = target ` `            ``// then print the left value and right value ` `            ``// Delete the last value of left and right list ` `            ``// and make the left execution to right side ` `            ``// and right side execution to left side ` `            ``else`  `            ``{ ` `                ``Console.WriteLine(LeftNode.data + ``" "` `+ RightNode.data); ` ` `  `                ``RightList.RemoveAt(RightList.Count - 1); ` `                ``LeftList.RemoveAt(LeftList.Count - 1); ` `                ``curr_left = LeftNode.right; ` `                ``curr_right = RightNode.left; ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main(String[] b) ` `    ``{ ` ` `  `        ``Node root = ``null``; ` `        ``root = AddNode(root, 2); ` `        ``root = AddNode(root, 6); ` `        ``root = AddNode(root, 5); ` `        ``root = AddNode(root, 3); ` `        ``root = AddNode(root, 4); ` `        ``root = AddNode(root, 1); ` `        ``root = AddNode(root, 7); ` `        ``int` `sum = 8; ` `        ``TargetPair(root, sum); ` `    ``} ` `} ` ` `  `/* This code contributed by PrinciRaj1992 */`

Output:

```1 7
2 6
3 5
```

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.

Improved By : princiraj1992