碰见了就刷一次,去吃午饭
/**
* 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) {
if (root == null || root == p || root == q) {
return root;
}
TreeNode ln = lowestCommonAncestor(root.left, p, q);
TreeNode rn = lowestCommonAncestor(root.right, p, q);
if (ln != null && rn != null) {
return root;
}
if (ln != null) {
return ln;
}
if (rn != null) {
return rn;
}
return null;
}
}