-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathPairSumBST.java
48 lines (45 loc) · 1.21 KB
/
PairSumBST.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package SummerTrainingGFG.BinarySearchTree;
import java.util.HashSet;
/**
* @author Vishal Singh
* 26-12-2020
*/
public class PairSumBST {
static class Node{
int data;
Node left,right;
Node(int data){
this.data = data;
}
}
Node root;
static HashSet<Integer> set = new HashSet<>();
static void findPairSum(Node root,int sum){
System.out.println(inOrder(root, sum));
}
static boolean inOrder(Node root, int sum){
if (root == null){
return false;
}
if(inOrder(root.left,sum)){
return true;
}
if (set.contains(sum - root.data)){
return true;
}else {
set.add(root.data);
}
return inOrder(root.right,sum);
}
public static void main(String[] args){
PairSumBST tree = new PairSumBST();
tree.root = new Node(20);
tree.root.left = new Node(8);
tree.root.right = new Node(22);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(12);
tree.root.left.right.left = new Node(10);
tree.root.left.right.right = new Node(14);
findPairSum(tree.root, 34);
}
}