235. Lowest Common Ancestor of a Binary Search Tree#
1class Solution {
2 public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
3 if (p.val < root.val && q.val < root.val) {
4 return lowestCommonAncestor(root.left, p, q);
5 } else if (p.val > root.val && q.val > root.val) {
6 return lowestCommonAncestor(root.right, p, q);
7 }
8 return root;
9 }
10}