-
Notifications
You must be signed in to change notification settings - Fork 93
/
LowestCommonAncestor
151 lines (125 loc) · 3.34 KB
/
LowestCommonAncestor
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package questions.saurabh;
/**
* <b>IDeserve <br>
* <a href="https://www.youtube.com/c/IDeserve">https://www.youtube.com/c/IDeserve</a>
* <br><br>
* Lowest Common Ancestor of 2 nodes in a Binary Tree</b><br>
* Given a binary tree and 2 tree nodes A and B, find the lowest common ancestor of the nodes.
* <br><br>
* <a href="https://www.youtube.com/watch?v=NBcqBddFbZw">Lowest Common Ancestor Youtube Link</a>
* @author Saurabh
*
*/
public class LowestCommonAncestor {
private TreeNode root;
public TreeNode getRoot() {
return root;
}
public TreeNode getLowestCommonAncestor(TreeNode A, TreeNode B) {
return getLowestCommonAncestor(root, A, B);
}
private TreeNode getLowestCommonAncestor(TreeNode curr, TreeNode A, TreeNode B) {
if(curr == null) {
return null;
}
// If we find p or q, we return the node
if(curr == A || curr == B)
return curr;
// Recursive calls to find LCA in left and right subtrees
TreeNode leftNode = getLowestCommonAncestor(curr.getLeft(), A, B);
TreeNode rightNode = getLowestCommonAncestor(curr.getRight(), A, B);
// If we found p and q in left or right subtree of the current node,
// this means current node is a common ancestor, so return the node
if(leftNode != null && rightNode != null)
return curr;
// If we found p or q in left or right subtree of the current node,
// the means current node is an ancestor, return the node
if(leftNode == null) {
return rightNode;
} else {
return leftNode;
}
}
/**
* Defines a tree node
* @author Saurabh
*
*/
class TreeNode {
private int data;
private TreeNode left;
private TreeNode right;
public TreeNode(int data, TreeNode left, TreeNode right) {
super();
this.data = data;
this.left = left;
this.right = right;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public TreeNode getLeft() {
return left;
}
public void setLeft(TreeNode left) {
this.left = left;
}
public TreeNode getRight() {
return right;
}
public void setRight(TreeNode right) {
this.right = right;
}
public TreeNode() {
super();
}
public TreeNode(int data) {
super();
this.data = data;
}
@Override
public String toString() {
return data+"";
}
}
/*
* *******************************************
* Methods for testing getLowestCommonAncestor
* *******************************************
*/
public static void main(String[] args) {
LowestCommonAncestor tree = new LowestCommonAncestor();
tree.createSampleTree();
TreeNode A = tree.getRoot().getLeft().getLeft(); // Node 4
TreeNode B = tree.getRoot().getRight(); // Node 3
TreeNode lca = tree.getLowestCommonAncestor(A, B);
System.out.println("LCA of " + A.getData() + " and " + B.getData() + " is " + lca);
}
/*
* Create a sample tree
* 1
* 2 3
* 4 5 6 7
*
*/
public void createSampleTree() {
root = new TreeNode(1, new TreeNode(2, new TreeNode(4), new TreeNode(5)), new TreeNode(3, new TreeNode(6), new TreeNode(7)));
}
/*
* Print inorder traversal
*/
public void printInorder() {
printInorder(root);
}
private void printInorder(TreeNode root) {
if(root == null) {
return;
}
printInorder(root.getLeft());
System.out.print(root.getData() + " ");
printInorder(root.getRight());
}
}