应该在二叉树的最近公共祖先之前写= = 需要用到二叉搜索树的特性 ~ 分成四种情况即可,具体见代码注释 时间复杂度 O(logn) 
class  Solution  { public  TreeNode  lowestCommonAncestor ( TreeNode  root,  TreeNode  p,  TreeNode  q)  { if ( root ==  p ||  root ==  q) { return  root; } if ( p. val <  root. val &&  q. val <  root. val) { return  lowestCommonAncestor ( root. left,  p,  q) ; } if ( p. val >  root. val &&  q. val >  root. val) { return  lowestCommonAncestor ( root. right,  p,  q) ; } return  root; } 
} 
class  Solution  { public  TreeNode  lowestCommonAncestor ( TreeNode  root,  TreeNode  p,  TreeNode  q)  { if ( root ==  p ||  root ==  q) { return  root; } if ( p. val <  root. val &&  q. val <  root. val) { return  lowestCommonAncestor ( root. left,  p,  q) ; } if ( p. val >  root. val &&  q. val >  root. val) { return  lowestCommonAncestor ( root. right,  p,  q) ; } return  root; } 
} 
注意:与二叉树的最近公共祖先 不同,这里可以利用二叉搜索树 的性质 当然,用二叉树来的方法也可以。 class  Solution  { public  TreeNode  lowestCommonAncestor ( TreeNode  root,  TreeNode  p,  TreeNode  q)  { if ( root ==  p ||  root ==  q)  { return  root; } 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) ; } return  root; } 
}