-
Notifications
You must be signed in to change notification settings - Fork 1
/
LowestCommonAncestor.java
53 lines (45 loc) · 1.68 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
import java.util.*;
public class LowestCommonAncestor {
public static void main(String[] args) {
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.right.left = new TreeNode(5);
root.right.right = new TreeNode(6);
TreeNode p = root.right.left;
TreeNode q = root.right.right;
System.out.println(lowestCommonAncestorIt(root,p,q).val);
}
public static TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null || root == p || root == q) return root;
TreeNode left = lowestCommonAncestor(root.left, p, q);
TreeNode right = lowestCommonAncestor(root.right, p, q);
if(left != null && right != null) return root;
return left != null ? left : right;
}
public static TreeNode lowestCommonAncestorIt(TreeNode root, TreeNode p, TreeNode q) {
Map<TreeNode, TreeNode> parent = new HashMap<>();
Queue<TreeNode> queue = new LinkedList<>();
parent.put(root, null);
queue.add(root);
while (!parent.containsKey(p) || !parent.containsKey(q)) {
TreeNode node = queue.poll();
if (node != null) {
parent.put(node.left, node);
parent.put(node.right, node);
queue.add(node.left);
queue.add(node.right);
}
}
Set<TreeNode> set = new HashSet<>();
while (p != null) {
set.add(p);
p = parent.get(p);
}
while (!set.contains(q)) {
q = parent.get(q);
}
return q;
}
}