forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLowestCommonAncestorOfBinarySearchTree.java
51 lines (46 loc) · 1.46 KB
/
LowestCommonAncestorOfBinarySearchTree.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
public class LowestCommonAncestorOfBinarySearchTree {
/**
* 假如不保证树中有p和q存在呢?
*/
// 耗时9ms
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (p.val < root.val && q.val < root.val) {
return lowestCommonAncestor(root.left, p, q);
} else if (p.val > root.val && q.val > root.val) {
return lowestCommonAncestor(root.right, p, q);
} else {
return root;
}
}
public TreeNode lowestCommonAncestor2(TreeNode root, TreeNode p, TreeNode q) {
if (!checkExist(root, p) || !checkExist(root, q)) {
throw new IllegalArgumentException("Not exist!!");
}
while (root != null) {
if (p.val < root.val && q.val < root.val) {
root = root.left;
} else if (p.val > root.val && q.val > root.val) {
root = root.right;
} else {
break;
}
}
return root;
}
/**
* 如何判断p或q一定存在,如果是BST就很简单
*/
private boolean checkExist(TreeNode root, TreeNode node) {
TreeNode cur = root;
while (cur != null) {
if (node.val > cur.val) {
cur = cur.right;
} else if (node.val < cur.val) {
cur = cur.left;
} else {
return true;
}
}
return false;
}
}