> For the complete documentation index, see [llms.txt](https://zedive.gitbook.io/project-l/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zedive.gitbook.io/project-l/part-1/basic_data_structure/binary_tree/binary_search_tree/lca-in-bst.md).

# LCA in BST

## Question ([LC.235](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/))

How can you use the sorted property of BST?

## Code

```java
// if LCA is guaranteed to exist, ez then
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
    if (Math.min(p.val, q.val) > root.val) {
        return lowestCommonAncestor(root.right, p, q);
    } else if (Math.max(p.val, q.val) < root.val) {
        return lowestCommonAncestor(root.left, p, q);
    } else {
        return root;
    }
}
```
