PROBLEM DESCRIPTION
Find the lowest common ancestor in an unordered binary tree A, given two values, B and C, in the tree. Leetcode
SOLUTION
- Get root to node path: YouTube
- In the previous step, there is a chance that one of the Node is not present, return null in that case
- Compare the paths stored in two lists to find the closest one.
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
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null) return null;
List<TreeNode> listA = new ArrayList<>();
boolean checkA = solve(root, p, listA);
List<TreeNode> listB = new ArrayList<>();
boolean checkB = solve(root, q, listB);
if(!checkA || !checkB) return null;
return common(listA, listB);
}
public boolean solve(TreeNode A, TreeNode k, List<TreeNode> list){
if(A == null) return false;
list.add(A);
if(A.val == k.val) return true;
if(solve(A.left, k, list) || solve(A.right, k, list)){
return true;
}
list.remove(list.size()-1);
return false;
}
public TreeNode common(List<TreeNode> l1, List<TreeNode> l2){
int i=0;
while(i<l1.size() && i<l2.size() && l1.get(i).val == l2.get(i).val){
i++;
}
return l1.get(i-1);
}
}