题目要求的是深度最大的祖先结点!!!而且它俩一定存在公共祖先结点
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root==null) return root;
//因为按题目说明来看它俩一定有公共祖先结点,所有当其中一个为根节点时,则它俩深度最大的公共祖先结点一定是这个根节点了
if(root==p||root==q) return root;
TreeNode left = lowestCommonAncestor(root.left,p,q);
TreeNode right = lowestCommonAncestor(root.right,p,q);
if(left!=null&&right!=null) return root;
if(left!=null) return left;
if(right!=null) return right;
return null;
}
}