-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion235.java
More file actions
76 lines (68 loc) · 2.1 KB
/
Copy pathQuestion235.java
File metadata and controls
76 lines (68 loc) · 2.1 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
import java.util.*;
public class Question236
{
public static void getPath (TreeNode root, List<TreeNode> result, TreeNode node)
{
if(root!=null)
{
result.add(root);
if(root.val == node.val)
return;
else if(root.val>node.val)
getPath(root.left,result,node);
else
getPath(root.right,result,node);
}
}
public static TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q)
{
List<TreeNode> node1List = new ArrayList<>();
List<TreeNode> node2List = new ArrayList<>();
getPath(root,node1List,p);
getPath(root,node2List,q);
System.out.println(node1List);
System.out.println(node2List);
TreeNode result = null;
int minValue = Integer.MAX_VALUE;
if(node1List.size()<node2List.size())
{
for(TreeNode tn:node1List)
{
if(node2List.contains(tn))
{
result = tn;
}
}
}
else
{
for(TreeNode tn1:node2List)
{
if(node1List.contains(tn1))
{
result = tn1;
}
}
}
return result;
}
public static void main(String[] args)
{
TreeNode root = new TreeNode(10,null,null);
TreeNode l1 = new TreeNode(7,null,null);
TreeNode r1 = new TreeNode(12,null,null);
TreeNode ll2 = new TreeNode(-3,null,null);
TreeNode rr2 = new TreeNode(20,null,null);
TreeNode last = new TreeNode(33,null,null);
TreeNode lastnew = new TreeNode(123,null,null);
root.left = l1;
root.right = r1;
l1.left = ll2;
r1.right = rr2;
rr2.right = last;
last.right = lastnew;
Trees.display(root);
TreeNode result = lowestCommonAncestor(root,l1,r1);
System.out.println("LCA : "+result.val);
}
}