---
# System prepended metadata

title: 0235. Lowest Common Ancestor of a Binary Search Tree
tags: [Leetcode, LCA, Easy]

---

# 0235. Lowest Common Ancestor of a Binary Search Tree
###### tags: `Leetcode` `Easy` `LCA` 
Link: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
## 思路
如果root的值在p的值和q的值的范围内，就返回root
- 如果root的值大于q的值，就在root.left里面找
- 如果root的值小于p的值，就在root.right里面找
## Code
```java=
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        TreeNode curr;
        if(p.val > q.val) curr = findLCA(root, q, p);
        else curr = findLCA(root, p, q);
        return curr;
    }
    public TreeNode findLCA(TreeNode root, TreeNode p, TreeNode q){
        if(root == null) return null;
        if(root.val>=p.val && root.val<=q.val){
            return root;
        }
        else if(root.val>p.val){
            return findLCA(root.left, p, q);
        }
        else{
            return findLCA(root.right, p, q);
        }
    }
}
```