```
public class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
int mid=root.val;
int pv=p.val;
int qv=q.val;
TreeNode min=pv<qv?p:q;
TreeNode max=pv>qv?p:q;
if(min.val<=mid&&max.val>=mid)
return root;
if(max.val<mid)
return lowestCommonAncestor(root.left,p,q);
else if(min.val>mid)
return lowestCommonAncestor(root.right,p,q);
return null;
}
```

}