Skip to content

Commit 2cb3210

Browse files
authored
Create Pair sum in a BST
1 parent 27ea0a2 commit 2cb3210

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
Given a binary search tree and an integer S, find pair of nodes in the BST which sum to S. You can use extra space of the order of O(log n).
3+
Note:
4+
1. Assume BST contains all unique elements.
5+
2. In a pair, print the smaller element first.
6+
7+
Input Format :
8+
The first line of input contains data of the nodes of the tree in level order form. The data of the nodes of the tree is separated by space. If any node does not have left or right child, take -1 in its place. Since -1 is used as an indication whether the left or right nodes exist, therefore, it will not be a part of the data of any node.
9+
The following line of input contains an integer, that denotes the value of S.
10+
11+
Output format:
12+
You have to print each pair in a different line (pair elements separated by space). The order of different pairs, to be printed, does not matter.
13+
14+
Constraints:
15+
Time Limit: 1 second
16+
17+
Sample Input 1:
18+
8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1
19+
12
20+
Sample Output 1:
21+
2 10
22+
5 7
23+
*/
24+
import java.util.*;
25+
public class Solution {
26+
/*
27+
* BinaryTreeNode class
28+
*
29+
* class BinaryTreeNode<T>
30+
* {
31+
* T data;
32+
* BinaryTreeNode<T> left;
33+
* BinaryTreeNode<T> right;
34+
* public BinaryTreeNode(T data)
35+
* {
36+
* this.data = data;
37+
* }
38+
* }
39+
*/
40+
41+
public static void printNodesSumToS(BinaryTreeNode<Integer> root, int s) {
42+
// Write your code here
43+
if (root==null)
44+
return;
45+
else
46+
{
47+
ArrayList<Integer> arr = convertToArray(root);
48+
Collections.sort(arr);
49+
//for (int i=0;i<arr.size();i++)
50+
//{
51+
// System.out.print (arr.get(i)+" ");
52+
//}
53+
//System.out.println();
54+
printPairSum(arr,s);
55+
}
56+
57+
}
58+
59+
private static ArrayList<Integer> convertToArray(BinaryTreeNode<Integer> root)
60+
{
61+
if (root==null)
62+
{
63+
ArrayList<Integer> arr = new ArrayList<Integer>();
64+
return arr;
65+
}
66+
67+
68+
ArrayList<Integer> currArr = new ArrayList<Integer>();
69+
ArrayList<Integer> leftArr = convertToArray(root.left);
70+
if (!leftArr.isEmpty())
71+
{
72+
currArr.addAll(leftArr);
73+
}
74+
75+
currArr.add(root.data);
76+
77+
ArrayList<Integer> rightArr = convertToArray(root.right);
78+
if (!rightArr.isEmpty())
79+
{
80+
currArr.addAll(rightArr);
81+
}
82+
return currArr;
83+
}
84+
85+
private static void printPairSum(ArrayList<Integer> arr, int s)
86+
{
87+
int i=0,j=arr.size()-1;
88+
while(i<j)
89+
{
90+
int val1=arr.get(i);
91+
int val2=arr.get(j);
92+
if (val1+val2>s)
93+
{
94+
j=j-1;
95+
}
96+
else if(val1+val2<s)
97+
{
98+
i=i+1;
99+
}
100+
else
101+
{
102+
System.out.println(val1+" "+val2);
103+
i=i+1;
104+
j=j-1;
105+
}
106+
107+
}
108+
}
109+
110+
}

0 commit comments

Comments
 (0)