Below is my clean Java solution that assumes following definition of BST:-

left < root < right

```
public class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
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;
}
}
```

Now, my question:-

Many definitions of BST is like following (one that allows duplicate values) :-

left <= root < right. This too is a valid BST.

However, the solution above will fail with this definition of BST. Below is the counter example :-

```
2
/
2
/
2
```

If p and q are 2's on level 1 and level 2, above solution will return root (i.e. node at level 0) as result. However, actual answer is the one at level 1. How to deal with this situation?