-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLowestCommonAncestorOfABinaryTree.java
60 lines (56 loc) · 1.89 KB
/
LowestCommonAncestorOfABinaryTree.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
// https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
// #tree
/**
* Definition for a binary tree node. public class TreeNode { int val; TreeNode left; TreeNode
* right; TreeNode(int x) { val = x; } }
*/
class Solution {
/* better idea: use state: 0 (not found both p,q), 1 (found p or q), 2 (found q)
// binary approach: => 0(n)
-> check left side:
if state = 0,
-> result will be in right side
if state = 1,
-> return parent node
if state = 2, no not need to check right side.
*/
// idea: find parent path from root to p
// idea: find parent path from root to q.
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
HashMap<Integer, TreeNode> mapResult = preorderTraversal(root);
HashSet<Integer> parentOfPSet = new HashSet<>();
parentOfPSet.add(p.val);
TreeNode parentOfP = mapResult.get(p.val);
while (parentOfP != null) {
parentOfPSet.add(parentOfP.val);
parentOfP = mapResult.get(parentOfP.val);
}
if (parentOfPSet.contains(q.val)) {
return q;
} else {
TreeNode parentOfQ = mapResult.get(q.val);
while (parentOfQ != null) {
if (parentOfPSet.contains(parentOfQ.val)) {
return parentOfQ;
}
parentOfQ = mapResult.get(parentOfQ.val);
}
return root;
}
}
public HashMap<Integer, TreeNode> preorderTraversal(TreeNode root) {
HashMap<Integer, TreeNode> result = new HashMap<>();
invokePreOrderTraversal(root, result, null);
return result;
}
private void invokePreOrderTraversal(
TreeNode root, HashMap<Integer, TreeNode> result, TreeNode p) {
// postorder: left -> right -> root
if (root == null) {
return;
}
result.put(root.val, p);
invokePreOrderTraversal(root.left, result, root);
invokePreOrderTraversal(root.right, result, root);
}
}