-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathLowestCommonAncestor.java
81 lines (69 loc) · 1.93 KB
/
LowestCommonAncestor.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
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
package SummerTrainingGFG.Tree;
import java.util.ArrayList;
/**
* @author Vishal Singh
*/
public class LowestCommonAncestor {
static class Node {
int key;
Node left;
Node right;
Node(int key) {
this.key = key;
}
}
static boolean findPath(Node root, ArrayList<Node> path, int nm) {
if (root == null) {
return false;
}
path.add(root);
if (root.key == nm) {
return true;
}
if (findPath(root.left, path, nm) || findPath(root.right, path, nm)) {
return true;
}
path.remove(path.size() - 1);
return false;
}
static Node lowestCommonAncestor(Node root, int n, int m) {
ArrayList<Node> path1 = new ArrayList<>();
ArrayList<Node> path2 = new ArrayList<>();
if (!findPath(root, path1, n) || !findPath(root, path2, m)) {
return null;
}
for (int i = 0; i < path1.size() && i < path2.size(); i++) {
if (path1.get(i + 1) != path2.get(i + 1)) {
return path1.get(i);
}
}
return null;
}
public static void main(String[] args) {
Node t = new Node(10);
t.left = new Node(50);
t.right = new Node(60);
t.left.left = new Node(70);
t.left.right = new Node(20);
t.left.left.left = new Node(40);
t.left.right.left = new Node(90);
t.left.right.left.left = new Node(30);
t.left.right.right = new Node(80);
/*
{10}
/ \
{50} {60}
/ \
{70} {20}
/ / \
{40} {90} {80}
/
{30}
*/
Node lca = lowestCommonAncestor(t,30,80);
if (lca==null)
System.out.println("Not found");
else
System.out.println(lca.key);
}
}