-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathSolution.java
46 lines (46 loc) · 1.24 KB
/
Solution.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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
String sp=f(root,p);
sp=sp.substring(0,sp.length()-1);
String sq=f(root,q);
sq=sq.substring(0,sq.length()-1);
for (int i = 0; i < Math.min(sp.length(), sq.length()); i++) {
String kp=sp.substring(i,i+1);
String kq=sq.substring(i,i+1);
if (kp.equals(kq)) {
if (kp.equals("0")) {
root=root.left;
}else {
root=root.right;
}
}else {
return root;
}
}
return root;
}
public String f(TreeNode root, TreeNode p){
if (root==null) {
return "3";
}
if (root==p) {
return "2";
}
String left=f(root.left,p);
String right=f(root.right,p);
if (left.substring(left.length()-1,left.length()).equals("2")) {
return "0"+left;
}else {
return "1"+right;
}
}
}