```
public class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(p.val>q.val) return lowestCommonAncestor(root,q,p);
if(root==null) return null;
if(p.val>root.val) return lowestCommonAncestor(root.right,p,q);
if(q.val<root.val) return lowestCommonAncestor(root.left,p,q);
return root;
}
```

}