-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathBT_Problem_31.java
More file actions
76 lines (56 loc) · 1.53 KB
/
Copy pathBT_Problem_31.java
File metadata and controls
76 lines (56 loc) · 1.53 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
66
67
68
69
70
71
72
73
74
75
76
package trees.binaryTree;
import java.util.*;
/*
* Title: Find LCA in a Binary tree
*/
class Solution{
@SuppressWarnings("unused")
private BT_Problem_31 ans;
public Solution() {
this.ans = null;
}
@SuppressWarnings("unused")
private boolean recurseTree(BT_Problem_31 currentNode, BT_Problem_31 p, BT_Problem_31 q) {
if(currentNode == null)
return false;
int left = this.recurseTree(currentNode.left, p, q) ? 1 : 0;
int right = this.recurseTree(currentNode.right, p, q) ? 1 : 0;
int mid = (currentNode == p || currentNode == q) ? 1 : 0;
if(mid + left + right >= 2)
this.ans = currentNode;
return (mid + left + right > 0);
}
public BT_Problem_31 lowestCommonAncestor(BT_Problem_31 root, BT_Problem_31 p, BT_Problem_31 q) {
Deque<BT_Problem_31> stack = new ArrayDeque<>();
Map<BT_Problem_31, BT_Problem_31> parent = new HashMap<>();
parent.put(root, null);
stack.push(root);
while(!parent.containsKey(p) || !parent.containsKey(q)) {
BT_Problem_31 node = stack.pop();
if(node.left != null) {
parent.put(node.left, node);
stack.push(node.left);
}
if(node.right != null) {
parent.put(node.right, node);
stack.push(node.right);
}
}
Set<BT_Problem_31> ancestors = new HashSet<>();
while(p != null) {
ancestors.add(p);
p = parent.get(p);
}
while(!ancestors.contains(q))
q = parent.get(q);
return q;
}
}
public class BT_Problem_31{
int val;
BT_Problem_31 left;
BT_Problem_31 right;
BT_Problem_31(int x){
val = x;
}
}