-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCABinarySearchTree.java
More file actions
66 lines (53 loc) · 1.92 KB
/
Copy pathLCABinarySearchTree.java
File metadata and controls
66 lines (53 loc) · 1.92 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//Assuming both the input nodes are present in the BST
public class LCABinarySearchTree {
static int noOfNodesPresent;
public static void main(String[] args){
//Construct a binary search tree
BinaryTree<Integer> bt1=new BinaryTree<Integer>();
bt1.setData(8);
BinaryTree<Integer> bt2=new BinaryTree<Integer>();
bt2.setData(6);
BinaryTree<Integer> bt3=new BinaryTree<Integer>();
bt3.setData(12);
BinaryTree<Integer> bt4=new BinaryTree<Integer>();
bt4.setData(4);
BinaryTree<Integer> bt5=new BinaryTree<Integer>();
bt5.setData(7);
BinaryTree<Integer> bt6=new BinaryTree<Integer>();
bt6.setData(10);
BinaryTree<Integer> bt7=new BinaryTree<Integer>();
bt7.setData(15);
bt1.setLeftChild(bt2);
bt1.setRightChild(bt3);
bt2.setLeftChild(bt4);
bt2.setRightChild(bt5);
bt3.setLeftChild(bt6);
bt3.setRightChild(bt7);
////////////////////////////////////////////////////////////
checkPresence(bt1,bt6,bt7);
if(noOfNodesPresent==2)
System.out.println(findLCA(bt1,bt6,bt7).getData());
else if(noOfNodesPresent==1)
System.out.println("Only one node present!! So no LCA");
else
System.out.println("No Node Present!!!");
}
static void checkPresence(BinaryTree<Integer> root,BinaryTree<Integer> node1,BinaryTree<Integer> node2){
if (root==null)
return;
if (root.getData()==node1.getData()||root.getData()==node2.getData()){
noOfNodesPresent++;
}
checkPresence(root.getLeftChild(),node1,node2);
checkPresence(root.getRightChild(), node1, node2);
}
static BinaryTree<Integer> findLCA(BinaryTree<Integer> root,BinaryTree<Integer> node1,BinaryTree<Integer> node2){
if(root==null)
return null;
if(root.getData()<node1.getData() && root.getData()<node2.getData())
return findLCA(root.getRightChild(),node1,node2);
else if(root.getData()>node1.getData() && root.getData()>node2.getData())
return findLCA(root.getLeftChild(),node1,node2);
return root;
}
}