-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode235.java
More file actions
36 lines (33 loc) · 1.29 KB
/
LeetCode235.java
File metadata and controls
36 lines (33 loc) · 1.29 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
import util.TreeNode;
public class LeetCode235 {
public static void main(String[] args) {
TreeNode root;
// 输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
// 输出: 6
root = TreeNode.buildTree(new Integer[] { 6, 2, 8, 0, 4, 7, 9, null, null, 3, 5 });
System.out.println(new Solution235().lowestCommonAncestor(root, root.left, root.right).val);
// 输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
// 输出: 2
root = TreeNode.buildTree(new Integer[] { 6, 2, 8, 0, 4, 7, 9, null, null, 3, 5 });
System.out.println(new Solution235().lowestCommonAncestor(root, root.left, root.left.right).val);
}
}
class Solution235 {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
TreeNode node = root;
while (node != null) {
if ((node.val < p.val && node.val > q.val) || node.val > p.val && node.val < q.val) {
break;
} else if (node.val == p.val || node.val == q.val) {
break;
} else {
if (node.val < p.val && node.val < q.val) {
node = node.right;
} else {
node = node.left;
}
}
}
return node;
}
}