-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBM38.java
More file actions
26 lines (23 loc) · 908 Bytes
/
BM38.java
File metadata and controls
26 lines (23 loc) · 908 Bytes
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
package NiukeTOP101;
import java.util.ArrayList;
public class BM38 {
public int lowestCommonAncestor (TreeNode root, int o1, int o2) {
return helper(root, o1, o2).val;
}
private TreeNode helper(TreeNode root, int o1, int o2) {
if(root == null || root.val == o1 || root.val == o2){
return root;
}
TreeNode left = helper(root.left, o1, o2);
TreeNode right = helper(root.right, o1, o2);
//如果left为空,说明这两个节点在root结点的右子树上,我们只需要返回右子树查找的结果即可
if (left == null)
return right;
//同上
if (right == null)
return left;
//如果left和right都不为空,说明这两个节点一个在root的左子树上一个在root的右子树上,
//我们只需要返回cur结点即可。
return root;
}
}