This problem can be solved by using the solution of 236. Lowest Common Ancestor of a Binary Tree.
While the tree here is a binary search tree, so the tree is sorted, we can have a simpler solution:
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(root.val>p.val&&root.val>q.val){ return lowestCommonAncestor(root.left, p, q); }else if(root.val<p.val&&root.val<q.val) return lowestCommonAncestor(root.right, p, q); return root; }